cancel
Showing results for 
Search instead for 
Did you mean: 

problem with ftl template.

need
Champ in-the-making
Champ in-the-making
Hi all,

I have a problem with a dashlet 3.4d with Alfresco Share. I have followed many tutorials but I can not solve my problem. Let me explain.

I created Alfresco webscripts makes me a search for all documents that have a certain aspect. and return a json and it works if I access the page http://localhost:8080/alfresco/service/let/modelli visualize the result in json format.


{"modelli" : [
        {"name" : "consulente.xls",
         "link" : "\/alfresco\/d\/d\/workspace\/SpacesStore\/b632375f-e6a1-40e2-a2ee-dd858a4f8dc2\/consulente.xls",
        }
]
}

Now I have created a dashlet of Alfresco Share, which should see the list of documents that have a certain aspect.

Post the code for clarity:

- getmodel.get.desc.xml

<webscript>
   <shortname>Crea da modello</shortname>
   <description>Nuovo documento da modello esistente</description>
   <!– one of dashlet, site-dashlet or user-dashlet –>
   <family>dashlet</family>
   <url>/components/dashlets/getmodel</url>
</webscript>

- getmodel.get.html.ftl

<div class="dashlet">
   <div class="title">Nuovo documento da modello esistente</div>
   <div class="body scrollableList">
     <div class="detail-list-item first-item last-item">
        <h5>Modelli di documento</h5>
      <table>
         <#list modelli as mod>
            <tr>
               <td><b>Name</b></td><td>${mod.name}</td>
            </tr>
            
            <tr>
            <td><b>Link</b></td><td><a href="${url.context}${mod.link}?guest=true">${url.context}${mod.link}</a></td>
            </tr>
         </#list>
      </table>
     </div>
   </div>
</div>

- getmodel.get.js


var connector = remote.connect("alfresco");
var data = connector.call("let/modelli");
if (data.status == 200) {
var result = eval('(' + data + ')');
model.modelli = result["modelli"];
}else{
   model.modelli="zzzz";
}

And this is  exception in tomcat log.

Exception: freemarker.template.TemplateException - Expected collection or sequence. modelli evaluated instead to freemarker.template.SimpleScalar on line 7, column 32 in org/alfresco/components/dashlets/getmodel.get.html.ftl.

I understand that the problem are in the ftl template file but I can not figure out how I can fix and what is wrong. Help me to understand?

Thank you all for the availability.
13 REPLIES 13

jordiv
Champ on-the-rise
Champ on-the-rise
It seems that your modelli variable isn't a list.
Have you tried printing its value?

Cheers,
Jordi.

need
Champ in-the-making
Champ in-the-making
no i don't have printed this variable? How can i print the variable in file log for example?

Thanks

need
Champ in-the-making
Champ in-the-making
the instructions var data = connector.call("let/modelli") what it returns? And in what format?

scouil
Star Contributor
Star Contributor
Or it could just be that  connector.call("let/modelli");  fails (doesn't return with a 200 status).
In this case you enter in the "else" where  model.modelli is assigned the string "zzzzzz"  which converts into a freemarker.template.SimpleScalar.
I'm not sure what you're trying to do with this but, even in the "else",  model.modelli should be an array or your html template will fail.

need
Champ in-the-making
Champ in-the-making
Ok I printed the variable (modelli) in the dashlet and i see zzzz, then it means that the state of the remote call is not 200. How can I fix this?

I have to change something in the connection string? (Let / modelli)?

Thank a lot.

jordiv
Champ on-the-rise
Champ on-the-rise
Try using get instead of call and put a "/" at the beginning:

var data = connector.get("/let/modelli");

It this doesn't work you can check what status code are you getting from your webscript.

Cheers,
Jordi.

scouil
Star Contributor
Star Contributor
To begin with, data.status doesn't directly returns the code but a ResponseStatus object ( http://wiki.alfresco.com/wiki/Surf_Platform_-_Freemarker_Template_and_JavaScript_API#ResponseStatus ).
So you should do
if (data.status.code == 200)
   instead of  
if (data.status == 200)

But I would advise you to return the response in the modelli whatever the code is:
var connector = remote.connect("alfresco");
var data = connector.call("let/modelli");
var result = eval('(' + data + ')');
model.modelli = result;
and handle the display in your html template

<#if modelli.status.code == 200>
    <table>
         <#list modelli.response["modelli"] as mod>
            <tr>
               <td><b>Name</b></td><td>${mod.name}</td>
            </tr>
            <tr>
            <td><b>Link</b></td><td><a href="${url.context}${mod.link}?guest=true">${url.context}${mod.link}</a></td>
            </tr>
         </#list>
    </table>
<#else>
    Error ${modelli.status.code}: <br />
    ${modelli.status.message}
</#if>
I haven't tested it so it might be full of syntax errors but I hope you get the point.

need
Champ in-the-making
Champ in-the-making
ok i ok I found the problem that did not entry in the IF block. I had to put a slash at the beginning of the string let / models (/ let / templates /).

But now I can print var data is returned:    {"modelli" : [ {"name" : "consulente.xls", "link" : "\/alfresco\/d\/d\/workspace\/SpacesStore\/b632375f-e6a1-40e2-a2ee-dd858a4f8dc2\/consulente.xls", } ] }

Now how do I access from ftl template at  variable name and link variable?

Thanks at all.

scouil
Star Contributor
Star Contributor
Hum… I'm OK to help when you're stuck but don't expect us to do your whole job.
How about you give it a try on your own and come back posting here if you're actually stuck? I bet you'll get it to work with the first thing you'll try.