Wednesday, July 9, 2014

Using Flex Assets in Templates and CSElements

Let’s assume that we want to use a FlexAsset in a template and we don’t have access to the id of the asset but only to the value of an attribute of the asset.  

We will use the Demo_M (DemoImage) asset type with the definition “DemoImageDefinition”






The attributes used in this definition are file and thumbnail of type BLOB and title, altText and description of type string, string and text.

We have a number of assets of this type and that are using the same definition:


The goal of this tutorial is to access the DemoImage assets using as the identifier for the asset the value of an attribute, let’s say title.

First we need to create a searchstate object:

<%-- Create a searchstate --%>
<searchstate:create name="img_title" />

To narrow the result set we have to provide a constraint for searchstate. We do this by adding a constraint:

<searchstate:addsimplestandardconstraint name="img_title" attribute="title" value="Earth" typename="Demo_A"/>

Next step is to create and assetset using searchstate:

<assetset:setsearchedassets name="imgByTitle" assettypes="Demo_M" constraint="img_title" fixedlist="true"/>


Retrive the wanted attribute (altText for this example):

<assetset:getattributevalues name="imgByTitle" listvarname="altTextList" attribute="altText" typename="Demo_A"/>

Display the attribute value:

<p>Alt text for Earth image is: <ics:listget fieldname="value" listname="altTextList"/></p>


If the attribute has multiple values (like the description attribute):

<assetset:getattributevalues name="imgByTitle" listvarname="descList" attribute="description" typename="Demo_A"/>
<p>
     Description:<br/>
     <ics:listloop listname="descList">
          <ics:listget fieldname="value" listname="descList"/><br/>
     </ics:listloop>
</p>
        

To get the values from asset type primary table (like id, name, etc…) we can use asset:search tag:

<ics:setvar name="prefix:name_op" value="="/>
<ics:setvar name="prefix:name" value="earth"/>
<asset:search prefix="prefix" list="earthAttributes" type="Demo_M"/>
<ics:listloop listname="earthAttributes">
     <ics:listget listname="earthAttributes" fieldname="id"/>----
     <ics:listget listname="earthAttributes" fieldname="name"/>
</ics:listloop>
</cs:ftcs>


The result of the execution of the template:


No comments: