How can I compile the contents of nested rudders?

The project I'm working on uses the template system Handlebars.js. It reads the content and when compiling the template, if necessary, adds the content:

<div class="content"> <p>lorem ipsum</p> {{{ content }}} </div> 

In this case, the rudders are compiled using a JS object that has a content property that is a string of text or HTML (hence, triple brackets).

However, it is possible that the value of the content (being text or HTML) may also include steering wheel interpolation code:

 var contentPassedToHandlebars = { content: '<p>{{ foobar }}</p>', foobar: 'foo' }; 

<p>{{ foobar }}</p> currently being issued, but what I would like to receive is <p>foo</p> .

Do rudders have tools for this nested content, or do they need a special assistant? ( {{{custom_parse content}}} )?

In the context of this issue

The situation obtained from the assembly system (metalmith), which reads as markdowns in files, converts them to HTML, appends the result to the content property of the file object, and then analyzes the rudder template, which injects file.content into it. All this and I hoped that there was a decision to put manual or string interpolations in markdowns so that label files could have access to the same variables that templates had access to (obviously, more global values ​​in config.json , and not values ​​related to created file object).

+5
source share
2 answers

There is no built-in way to do this. You will need to manage your own pre-rendering process or internal assistant.

For the solution, I completed the preliminary visualization before the official rendering. Although the code is not a wheel, but instead part of the metalsmith-templates plugin, here is the solution I used .

This roughly translates to:

 var contentPassedToHandlebars = { content: '<p>{{ foobar }}</p>', foobar: 'foo' }; var x = Handlebars.compile(contentPassedToHandlebars.content)(contentPassedToHandlebars); contentPassedToHandlebars.content = x; Handlebars.compile(realTemplateSource)(contentPassedToHandlebars); 
+4
source

OR use this:

metalsmith-in-place

 ``` index.js var inPlace = require('metalsmith-in-place') .... .use(inPlace(true)) ..... 

Now, if you write a {{> footer}} it will do its job.

+1
source

Source: https://habr.com/ru/post/1203871/


All Articles