by
KK » Sat Mar 17, 2018 2:37 am
Seeing @trendoman's interest in using embeds behave as functions, I have created something that might make things a tad easier.
The current version of Couch from GitHub (
https://github.com/CouchCMS/CouchCMS) now offers two new tags - <cms:func /> and <cms:call />.
Anyone willing to try out how the tags work, assuming you have the current GitHub version installed, try placing the following in any of your test template -
1.
- Code: Select all
<cms:func 'makecoffee' type='cappuccino' >
Making a cup of <cms:show type />.<br />
</cms:func>
<cms:call 'makecoffee' />
<cms:call 'makecoffee' '' />
<cms:call 'makecoffee' 'espresso' />
Output:
Making a cup of cappuccino.
Making a cup of .
Making a cup of espresso.
As you can see, the <cms:func> defines a function named 'makecoffee' which accepts an optional single parameter named 'type'.
The <cms:call /> statements that follow show how the function can be invoked. Please note how the default value of 'type' is used when the function is called without any parameter.
2.
- Code: Select all
<cms:func 'makecoffee2' type='cappuccino' size='medium'>
Making a <cms:show size /> cup of <cms:show type />.<br />
</cms:func>
<cms:call 'makecoffee2' />
<cms:call 'makecoffee2' 'espresso' 'large' />
<cms:call 'makecoffee2' size='small' type='espresso' />
Output:
Making a medium cup of cappuccino.
Making a large cup of espresso.
Making a small cup of espresso.
The next example above defines another function (named 'makecoffee2') that takes two parameters - 'type' and 'size'.
Please note the third <cms:call> statement above - it shows how we can pass parameters to the function in any order by explicitly using their names. If names are not used with the parameters then they have to be passed in strictly the same order as that defined by the function being called, as shown by the second <cms:call /> (i.e. first 'type' and then 'size').
3.
- Code: Select all
<cms:func 'recursion' count='0'>
<cms:if count lt '20'>
count: <cms:show count /><br>
<cms:call 'recursion' "<cms:add count '1' />" />
</cms:if>
</cms:func>
<cms:call 'recursion' />
Output:
count: 0
count: 1
count: 2
count: 3
count: 4
count: 5
..
..
Hopefully the simple examples above should be sufficient to illustrate how we can use <cms:func /> for creating resusable chunks of code.
I'd like to stress one point about <cms:func /> here -
every parameter we define for it needs to have a default value (it may be blank but it has to be present) e.g. the following is incorrect
- Code: Select all
<cms:func 'makecoffee' type >
Following is the right syntax if the parameter has no visible default value -
- Code: Select all
<cms:func 'makecoffee' type='' >
Finally, you might find it more manageable to put all functions (or groups of related functions) as snippets e.g. I could create a snippet named 'funcs.html' as follows (the cms:hide allows me to freely use text as comments) -
- Code: Select all
<cms:hide>
<!-- makes coffee (what else did you expect?) -->
<cms:func 'makecoffee' type='cappuccino' >
Making a cup of <cms:show type />.<br />
</cms:func>
<!-- make coffee version 2 -->
<cms:func 'makecoffee2' type='cappuccino' size='medium'>
Making a <cms:show size /> cup of <cms:show type />.<br />
</cms:func>
<!-- look Ma! I can recurse! -->
<cms:func 'recursion' count='0'>
<cms:if count lt '20'>
count: <cms:show count /><br>
<cms:call 'recursion' "<cms:add count '1' />" />
</cms:if>
</cms:func>
</cms:hide>
And now my test template could embed the snippet somewhere at its beginning -
- Code: Select all
<cms:embed 'funcs.html' />
and then call the functions as usual -
- Code: Select all
<cms:call 'makecoffee' />
<cms:call 'makecoffee' '' />
<cms:call 'makecoffee' 'espresso' />
<cms:call 'makecoffee2' />
<cms:call 'makecoffee2' 'espresso' 'large' />
<cms:call 'makecoffee2' size='small' type='espresso' />
<cms:call 'recursion' />
Hope this helps.