This small library is implemented to handle concurrent consent in arbitrary objects, operates on the alphabet with a simple syntax:
* `###varname{}` stores the variable content from this position until the occurence of the next
variable or a delimiter in between into the environments variable's stack
* `##varname{$value}` defines a value at this position
* `#varname{}` inserts the current value of varname from environment variables at this position
* A second brace pair behind a variable definition then transforms and formats with the script
and cacao environment variables, e.g. `##varname{#body{}}{#html_full{}}` replaces `#body{}`
with the values from `cacao.env.html_full` on template substitution.
To use the library, please import the index file
import 'cacao/index'
You can use the python file cacaodev.py
to compile the coffeescript files or simply run npm run dev
.
You might want to have a look at examples/
. You might for example define your html template like this:
html_tmpl = Cacao.Environment.Template.fromFile 'cacao/examples/html.cacao'
html_tmpl.readToEnv cacao.env
to setup the environment with one template only.
To get the content inside both html tags in the variable cacao.env.body_html
, you can work like this
html_text = """<div class="container">
Some text
</div>"""
html_tmpl.transform html_text, cacao.env
and you'll have the following variables in cacao.env
:
cacao.env.body_html == "Some text"
cacao.env.starttag == "div"
cacao.env.properties == """class="container""""
If using a programming or scripting language, it might be useful to know operands, tags and variables and transform them. In this example we'll see, how more dynamic Jinja could work.
Therefore there is a Cacao.Transition
class, that can be used to make a fourier transformation or to determine variables
just from there position in a string.
code_tmpl = Cacao.Transition.fromFile 'cacao/eamples/jinja_for.cacao'
code_tmpl.equiv.template_refactoring.readToEnv cacao.env
cacao.env.jinja_text = code_tmpl.template_refactoring.tmpl_str
{% for elem in elements %}
#body_jinja{}
{% endfor %}
code_tmpl.transform jinja_text, cacao.env
should then give the following variables in the environment
cacao.env.operand == "for"
cacao.env.what == "elem in elements"
cacao.env.varnames == "elem"
cacao.env.arrayname == "elements"
cacao.env.body_jinja == "#body_jinja{}"
As we can see, the next substitution would introduce everything from cacao.env.body_jinja
variable into the templates's string, so we could to some magic for dynamic loops.
cacao.env.html_full = """<#{cacao.env.starttag} #{cacao.env.properties}>#body_jinja{}</#{cacao.env.starttag}>"""
cacao.env.body_jinja = "#{cacao.env.hmtl_full}"
code_tmpl.transform jinja_text, cacao.env
would then give a html container per loop iteration in jinja and insert everything from jinja inside.
Mode and not -- more advanced examples and tests might follow.
Copyright 2024 Sebastian Lau <sebastianlau995@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.`