antonmedv / monkberry

Monkberry is a JavaScript library for building web user interfaces
https://monkberry.js.org
MIT License
1.49k stars 78 forks source link

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

Closed pikhovkin closed 6 years ago

pikhovkin commented 6 years ago

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 commented 6 years ago

https://github.com/monkberry/standalone

pikhovkin commented 6 years ago

@antonmedv, example, please

antonmedv commented 6 years ago

Read the docs

pikhovkin commented 6 years ago

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

antonmedv commented 6 years ago

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);

DeLaGuardo commented 6 years ago

@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

pikhovkin commented 6 years ago

@DeLaGuardo, exactly! Thanks!