There is a wide range of various projects and ideas for applications that come to mind where some kind of template system might be called for. One example of an application that came to mind for might right away is making a project that can be described as a static site generator. One of the aspects of a static site generator is that plain text, or markdown source files need to be converted into HTML and injected into an HTML page. However there are also all kinds of other application where a developer would want to go with at least some kine of template system.
1 - lodash template basic example
So a basic lodash template involves passing a string that will be the template as the first argument to the _.template method. This string can contain one or more delimiters that are used to inject data from an object when using the method that is returned when calling the _.template method.
In this basic example I am using the interpolate delimiter that is an equal sign before and opening pointy bracket and percent sign pattern. This kind of syntax is similar to ejs a popular template language used in node.js related projects as a way or writing templates.
2 - lodash template interpolate and escape delimiters
There is a significant dereference between using an equals (interpolate) sign and a dash (escape) when delimiters. For the most part when creating a template I would want to use the equal sign so that tags are made in a way that will render in the browser. However in some cases I would want to use a dash so that the resulting string is escaped.
So here I have some arrays of booleans and I want the result of an _.every call to be part of the result in the template.
So then what will be render as the result of a delimiter like that of the escape delimiter does not have to always be just an object property it can be the result of a method like _.every that returns true then all elements in an array are truthy.
3.2 - Looping and evaluate delimiters
4.1 - Back Ticks, AKA Template Literals, AKA Template Strings
One of many options to take into account would be Template Literals which might also often be referred to as Template strings, or just simply backticks. Although these might often be called a kind of string, they are not, and can not be used in with any kind of function that might call of a string. Also when creating a template function with a Template Literal a string value might not always be what is returned, that of course depends on how one goes about creating the final return value of the function just like any other kind of function.
For a simple hello world style example of Template Literals there is just creating an arrow function, or any kind of function for that matter and have a single argument for that function that will be a message. There is then using the return keyword in the body of the function to return a template literal starting with an opening back tick ` then before a closing back tick create what the template should be. When doing so I can define what is called a placeholders by using the dollar sign character and then place a value that I want in that are of the template between a set of opening and closing curly brackets.
5 - Conclusion
So the lodash template method is useful for creating and using templates. However it is not a replacement for other options when it comes to doing this sort of thing. In most applications I might use some dependency outside that of the lodash template method to preform these kinds of tasks. For example when it comes to parsing markdown into HTML I would use marked.js, and when it comes to parsing a JSON string into a workable object I would use the JSON.parse method, and the JSON.stringify method to do the inversion of this, that is turning an object into a JSON string.
When it comes to using nodejs and working out a sever side script of some kind I have to say that chances are I would not end up using the lodash template method to do so. If I am making a quick script from the ground up in a nodejs environment there are the native Template Literals that work well in just about all versions of node that I care about at this time. Also when it comes to working in a framework more often than not that framework would be express, and with that I would use a render engine like EJS with express as a template system.
When it comes to client side development I have come to like VUJS a whole lot, and of course when it comes to using that framework on the front end there is working with templates with vuejs. Yet another options when it comes to working in vuejs would be to use render functions that prove to be more flexible allowing for a great deal of control when creating a view for a data object in vuejs.