"Eric M. Ludlam" <eric@...> writes:
> On 01/07/2013 09:46 PM, joakim@... wrote:
>> Hello,
>>
>> My little srecode app is proceeding nicely, thanks!
>
> Hi,
>
> I'm glad things are working out with srecode. ;)
>
>> However, I need conditionals in a template and I can't figure out the
>> right way to do it.
>
> There is no conditional with an IF/ELSE/END type syntax. There are
> only sections, and the sections are either shown, or not shown. In
> addition, there is no NOT type operation.
>
> Of course, your examples show you know this, so I'm mostly making that
> clear for others following along.
>
>> Heres an example:
>> template af-classvar-declare :indent :blank
>> "declare a variable"
>> sectiondictionary "A"
>> ----
>> {{#A}}
>> /** {{?DOC}} */
>> private final {{?TYPE}} {{?NAME}};
>> {{/A}}
>> {{#ALIST}}
>> /** {{?DOC}} */
>> private final List<{{?TYPE}}> {{?NAME}};
>> {{/ALIST}}
>> ----
>> bind "e"
>>
>> heres another idea:
>>
>> template af-classvar-declare :indent :blank
>> "declare a variable"
>> sectiondictionary "A"
>> ----
>> {{#A}}
>> /** {{?DOC}} */
>> private final {{ISLIST}}List<{{/ISLIST}}{{?TYPE}}{{ISLIST}}>{{/ISLIST}} {{?NAME}};
>> {{/A}}
>> ----
>> bind "e"
>
> Either way you show should work fine. I think of it this way. If the
> text common between two forms is very similar, use the 2nd approach.
> if there is a bunch of differences, then go with the first method. If
> they are completely different, then just create 2 different templates
> all together.
>
>> neither attempt works very well when you loop it:
>> {{>A:classdecl:af-classvar-declare}}
>>
>> The dict is made like this:
>> (mapcar
>> (lambda (field)
>> (let ((subdict (srecode-dictionary-add-section-dictionary
>> newdict "A")))
>> (srecode-dictionary-set-value subdict "TYPE" (oref field :type))
>> (srecode-dictionary-set-value subdict "NAME" (oref field :name))
>> (srecode-dictionary-set-value subdict "DOC" (oref field :doc))
>> (if (oref field :is-list) (srecode-dictionary-set-value subdict "ISLIST" ""))
>
> Right here, I think you want to use `srecode-dictionary-show-section'.
>
> This is similar to srecode-dictionary-add-section-dictionary, but just
> for showing some section without looping.
>
>> ))
>> fieldlist)
>>
>> or this:
>> (mapcar
>> (lambda (field)
>> (let ((subdict (srecode-dictionary-add-section-dictionary
>> newdict (if (oref field :is-list) "ALIST" "A"))))
>
> You can use the srecode-dictionary-show-section here too. What you
> have should work though.
>
>> (srecode-dictionary-set-value subdict "TYPE" (oref field :type))
>> (srecode-dictionary-set-value subdict "NAME" (oref field :name))
>> (srecode-dictionary-set-value subdict "DOC" (oref field :doc))
>> ))
>> fieldlist)
>
> This one will get you in trouble since it will sort all your A's
> first, then all your ALISTS after.
>
> If you want them mixed together, you need use the the one where ISLIST
> is inline inside A.
>
> Make sure the sections that are looping are separate from the sections
> that are boolean (show/hide). Your second template should work well
> in that form as you can have many outer dictionaries A with inner show
> setting for ALIST.
>
> A good debug tip. Try out your template using just template settings
> before creating some lisp code. See the etc/srecode/tests.srt file
> for this example:
>
> template nested-dictionary-syntax-nesting
> section "TOP"
> show SHOW1
> set NAME "item1"
> section "SUB"
> show SHOW11
> set NAME "item11"
> end
> show SHOW2
> set NAME "item2"
> section "SUB"
> show SHOW21
> set NAME "item21"
> end
> show SHOW3
> set NAME "item3"
> section "SUB"
> show SHOW11
> set NAME "item31"
> section "SUB"
> show SHOW311
> set NAME "item311"
> end
> section "SUB"
> show SHOW321
> set NAME "item321"
> end
> end
> end
> ----
> $#TOP$$#SUB$$NAME$$#SUB$-$NAME$$/SUB$ $/SUB$$/TOP$
> ----
>
> Once you get comfortable with the structure, getting the lisp code to
> match is pretty easy.
>
> Good Luck
> Eric
Thanks Eric,
I seem to have gotten a simple example working now.
I had a couple of issues though.
this template seems to work:
template af-classvar-declare :indent :blank
"declare a variable"
sectiondictionary "A"
----
{{#A}}
/** {{?DOC}} */
private final {{#LIST}}List<{{/LIST}}{{?TYPE}}{{#LIST}}>{{/LIST}} {{?NAME}};
{{/A}}
----
bind "e"
with this code:
(mapcar
(lambda (field)
(let ((subdict (srecode-dictionary-add-section-dictionary
newdict "A")))
(srecode-dictionary-set-value subdict "TYPE" (oref field :type))
(srecode-dictionary-set-value subdict "NAME" (oref field :name))
(srecode-dictionary-set-value subdict "DOC" (oref field :doc))
(if (oref field :is-list) (srecode-dictionary-show-section subdict "LIST" )
(srecode-dictionary-hide-section subdict "LIST" ))
))
fieldlist)
The "hide" is probably not necessary, but I added it because I tested
many defferent variants, and most often the section I wanted to hide
got shown anyway.
template af-classvar-declare :indent :blank
"declare a variable"
sectiondictionary "A"
section "LIST"
end
----
{{#A}}
/** {{?DOC}} */
private final {{#LIST}}List<{{/LIST}}{{?TYPE}}{{#LIST}}>{{/LIST}} {{?NAME}};
{{/A}}
----
bind "e"
ISLIST showed up all the time which I found unexpected.
template af-classvar-declare :indent :blank
"declare a variable"
sectiondictionary "A"
section "LIST"
show ISLIST
end
----
{{#A}}
/** {{?DOC}} */
private final {{#LIST}}List<{{/LIST}}{{?TYPE}}{{#LIST}}>{{/LIST}} {{?NAME}};
{{/A}}
----
bind "e"
also got shown regardless of the value of ISLIST, also confusing.
I'll see if I can finish this in a way that can be published, for the
benefit of the list. (its in-house code but its a fairly basic pattern)
--
Joakim Verona
|