antonmedv/monkberry

How to render template by name (name has string type)?

pikhovkin opened this issue · 7 comments

How to render template by name (name has string type)?
I need to pass the name of the template as a string.

Example:

<script type="text/monkberry" id="Template">
	<h1>Hello {{ name }}!</h1>
</script>

var view = Monkberry.render('Template', document.body); // <-- 'Template'
view.update({name: 'World'});

@antonmedv, example, please

Read the docs

I didn't find this in docs. I would not ask if it was there.

Define template with text/monkberry.

<script type="text/monkberry" id="component">
    <h1>
        Hello, {{ name }}!
    </h1>
</script>

And you now can get access to component template.

var view = Monkberry.render(component, document.body);

@pikhovkin templates in monkberry-standalone evaluated in global scope. So you can render them with little trick: var view = Monkberry.render(window['Template'], document.body); If you need to identify them by strings

@DeLaGuardo, exactly! Thanks!