Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
#TODO:
Templ8 as you can probably guess is a JavaScript template engine, with a Django'ish style of syntax.
It's fast, light weight (5kb gzipped & minified) and unlike most other JavaScript template engines: Templ8 does not use the JavaScript with
statement . This actually makes Templ8 parse faster than it would if it did use the with
statement!
Templ8 does not restrict you to generating HTML. All outputs are strings so if you want to generate HTML, CSS, JavaScript or whatever, the choice is yours...
Templ8 is avaiable as a Node JS module as well as a browser micro-framework.
npm install Templ8
var Templ8 = require( 'Templ8' );
You have 2 options.
Templ8.client.js
is the version optimised for modern browsers (i.e. browsers that implement JavaScript version 1.6).Templ8.shimmed.js
contains shims to enable backwards compatibility with older browsers -- only an extra 0.5kb (min + gzip).NOTE: The shimmed version will still use JavaScript 1.6 features where available.
If all you want to do is swap out values you can use one of the following two smaller template functions.
:String
, param1:String
[, param2:String
, ..., paramN:String
] ):String
This function takes a minimum of two parameters. The first is the template you want perform substitutions over.
The template should use zero based tokens, e.g. {0}
, {1}
... {N}
that increment for each argument passed to the function.
e.g.
Templ8.format( 'Hello {0}! Nice {1} we\'re having.', 'world', 'day' );
returns: Hello world! Nice day we're having.
:String
, dict:Object
[, pattern:RegExp
] ):String
gsub works similarly to format only it takes an Object with the values you want to substitute, instead of a sequence of parameters. Actually format calls gsub internally.
e.g.
Templ8.gsub( 'Hello {name}! Nice {time} we\'re having.', { name : 'world', time : 'day' } );
returns: Hello world! Nice day we're having.
The default pattern for substitutions is /\{([^\}]+)\}/g
. However, you can supply a third argument to gsub which is your own custom pattern to use instead of the default.
If you want to do fancy stuff, you'll want to use the Templ8 constructor.
:String
, options:Object
)The Templ8 constructor actually takes an arbitrary number of String arguments which form the template body.
The last argument to the Templ8 can -- optionally -- be a configuration Object which defines any custom Filters you want to use for this Templ8 and any sub Templates it contains.
It also accepts the following four parameters (needless to say that these cannot be used as Filter names):
compiled | If this is set to true then the Templ8 will be compiled straight away, otherwise it will wait until the first time you call it's parse() method to compile. Default is false . |
fallback | This is the String to use as a fallback value in case any values are not present when parsing a Templ8 instance. Default is "" , Empty String. |
id | The ID of your Templ8. This is handy (and mandatory) if you want to use a Templ8 from within another Templ8. Otherwise an anonymous ID will be generated for your Templ8. |
logCompiledTpl | Useful for debugging. Set this to true to have the Templ8 method body logged to the console. Default is false . |
To keep it simple, a Templ8 instance only contains one method.
:Object
):String
This method accepts one parameter: an Object of values you want to substitute and returns a String of the parsed Templ8.
Any tokens in the Templ8 that do not have a dictionary value will use the fallback
value described above,
This is based on perl's $_
and is a reference to the the current dictionary value being parsed.
For instance if you are in a loop, rather than access the value using iter.current
you could also access it via $_
.
e.g. instead of this:
{[ iter.current|parse:'sub_template' for each ( items ) ]}
or this:
{[ item|parse:'sub_template' for each ( item in items ) ]}
you could do this:
{[ $_|parse:'sub_template' for each ( items ) ]}
This is the current iterator being parsed. It is an instance of an internal class called Iter. Iter instances are created internally, when you use a {% for %}
loop or an Array Comprehension {[ for each ]}
tag you should not need to create one yourself.
It has the following properties available for both Arrays and Objects:
count | the total number of all items in the Array or Object |
current | The current item being iterated over. |
first | The first item in the Array/ Object. Note: you cannot guarantee iteration order in an Object. |
index | The zero based index of the curent iteration. |
index1 | The one based index of thecurrent iteration. |
last | The last item in the Array/ Object. |
next | The next item in the iteration, or undefined if we're at the last item. |
parent | If you are in a nested loop and want to call the parent iter, you can access it via this property. |
previous | The previous item in the iteration, or undefined if we're at the first item. |
It has the following extra properties available for Objects:
firstKey | The key of the first item in the Object. |
lastKey | The key of the last item in the Object. |
key | The key of the current item being iterated over in the Object. |
nextKey | The next key in the iteration, or undefined if we're at the last item. |
previousKey | The previous key in the iteration, or undefined if we're at the first item. |
It also has the following two methods:
hasNext | returns true if there is a value after the current iteration to iterate over. Otherwise it will return false. |
stop | will stop the iterating, once it finishes it's current iteration. |
Along with the above Templ8 has some internal variables accessible for the more advanced user, should they require access to them.
Templ8 does not use the JavaScript with
statement. It implements its own version of a with
statement using an internal class called ContextStack.
It has five methods (you should NOT call these if you DO NOT know what you're doing):
current | returns the current context Object |
destroy | destroys the ContextStack. |
get | attempts to return the value of a dictionary Object, if it is in the ContextStack, otherwise it will return the fallback value or undefined. |
pop | removes the most recently added dictionary Object from the ContextStack. |
push | adds a dictionary Object to the ContextStack. |
This is where all parsed template output is stored. It is an instance of an internal class call Output.
It has two methods:
join | returns the output of the Templ8 instance. |
push | adds a String representation of the passed parameter to the Templ8 instance's output. |
This is a reference to Templ8.Assertions.
This is a reference to Templ8.Filters.
This is a reference to the internal utility functions used by Templ8.
var tpl = new Templ8( '{{value}}' );
tpl.parse( { value : 'github.com' } ); // returns: *github.com*
var tpl = new Templ8( '{{value|truncate:30|bold|link:"http://github.com"}}' );
tpl.parse( { value : 'github.com is great for sharing your code with other people.' } );
returns the String:
<a href="http://github.com"><strong>github.com is great for sharin...</strong></a>
var tpl = new Templ8( '{{value if value|notEmpty}}' );
tpl.parse( { value : 'github.com' } ); // returns: *github.com*
tpl.parse( {} ); // returns: empty String ( "" )
var tpl = new Templ8( '{{value unless value|equals:"foo"}}' );
tpl.parse( { value : 'github.com' } ); // returns: *github.com*
tpl.parse( { value : 'foo' } ); // returns: empty String ( "" )
var tpl = new Templ8(
'{% if value == "foo" || value == "bar" %}',
'{{value}}',
'{% elseif value != "lorem ipsum" %}',
'{{value|bold}}',
'{% elseif value|notEmpty %}',
'{{value|italics}}',
'{% else %}',
'No value',
'{% endif %}'
);
tpl.parse( { value : 'foo' } ); // returns: foo
tpl.parse( { value : 'lorem ipsum' } ); // returns: <strong>lorem ipsum</strong>
tpl.parse( { value : 'dolor sit amet' } ); // returns: <em>dolor sit amet</em>
tpl.parse( {} ); // returns: No Value
var tpl = new Templ8(
'{% for item in items %}',
'{{item}}',
'{% forempty %}',
'No items',
'{% endfor %}'
);
var tpl = new Templ8(
'{% sub sub_template_name %}',
'{{$_}}',
'{% endsub %}'
);
var tpl = new Templ8( '{[ v|parse"k" for each ( [k,v] in items ) if ( k|isTPL ) ]}' );
Allows you to execute arbitrary JavaScript.
var tpl = new Templ8( '{: aribtrarily.executing.nasty.code.isFun(); :}' );
Allows you to add comments in your template.
var tpl = new Templ8( '{# doing something complex and describing it is sensible, but not probable #}' );
(The MIT License)
Copyright © 2011 christos "constantology" constandinou http://muigui.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
JavaScript Client/ Server Template Engine
The npm package Templ8 receives a total of 21 weekly downloads. As such, Templ8 popularity was classified as not popular.
We found that Templ8 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.