[stringtemplate-interest] Indirect attribute lookup

Roman Odaisky roma at qwertty.com
Sun Sep 7 00:48:17 PDT 2008


Hi,

> > he’d write $something:img(alt={photo}, width={42}, height={42})$ and
> > that
> > would translate into obvious HTML.
>
> I do that all the time...wait...what's the 'something' for?

The something is an object from which the URL can be derived.

The actual template (img.st):

<img alt="$alt;null=name$" src="$it.url_prefix$/$thumb:
{$it$/}$$name;format={href}$$it.url_suffix$" $id:{id="$it" }$$class:
{class="$it" }$$
style:{style="$it" }$$title:{title="$it" }$$width:{width="$it" }$$height:
{height="$it" }$$extra:{$it$ }$/>

Actual invocation:

$property.allof_feature:
    {f | $f.allof_photo:
        {p | $p:img(name={$property$ - $f.type_or_other$}, thumb={100x100})$}
    $}
$

Result:

<img alt="42 Somestreet Rd. - Bedroom"
src="/media/property/photo/<uuid>/100x100/42%20Somestreet%20Rd.
%20-%20Bedroom.jpg" />

This has the advantage of filling the alt attribute with a sensible default 
value and providing a meaningful file name for those who might want to save 
it, as well as allowing the Web server to share the file without running the 
(expensive) middleware.

> I used to do img templates all the time...what do you need indirection
> for?

See the ugliness in the template definition above? Let’s imagine $(expr)$ 
looks for an attribute the name of which is equal to the value of expr. Then 
it can be rewritten:

<img alt="<stays>" src="<stays>"
$[id, class, style, title, width, height]:
    {attr | $(attr):{$attr$="$it$" }$}
$/>

Much more cleaner, and follows the DRY principle, which makes it more 
maintainable.

So can a way to perform such indirection be added? It looks quite useful for 
templates with implied formal arguments (those defined in files by 
themselves, as opposed to .stg files).

-- 
TIA
Roman.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2286 bytes
Desc: not available
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080907/e3553204/attachment.bin 


More information about the stringtemplate-interest mailing list