On 28/10/2011 00:39, Thorsten Scherler wrote:
Hi all,

what is the recommended way to implement presentation logic in c3? JX was our 
former choice but I could not find it in c3.

What should we use to implement simple presentation logic like we did with jx? 
What are people using? Or do I need to migrate jx?

Hi Thorsten,
this sounds like something we started discussing about a while ago [2]: as you can see from there, we ended up by not porting JX-related (or Velocity-related) components.

Personally, I think that StringTemplate should be enough for the moment; anyway, maybe not now, I still do believe that Cocoon 3 would really benefit from something like JX.

I tried to use the "string-template" component but I am running into a problem 
when using $if(boolean)$.

I am ATM integrating Apache Shiro, a security framework that performs 
authentication, authorization, cryptography, and session management, into c3 
and want to use something in the spirit of their taglibs
<shiro:notAuthenticated>
     Please<a href="login.jsp">login</a>.
</shiro:notAuthenticated>

So what I did is I wrote a ShiroInterpreter implements LanguageInterpreter that 
can extract this info.
However the interface LanguageInterpreter defines the return type to String.

<map:transform type="string-template">
   <map:parameter name="isUser" value="{shiro:authenticated}"/>
</map:transform>

will fail then interpreting
$if(!isUser)$
   <a href="/logout">/logout</a>
$else$
   <a href="/login">/login</a>
$endif$

with
...
Caused by: org.stringtemplate.v4.compiler.STException: null
        at 
org.stringtemplate.v4.compiler.Compiler.reportMessageAndThrowSTException(Compiler.java:213)
 ~[ST4-4.0.4.jar:na]

If I use only $isUser$ I get the correct value. Reading "Conditional 
subtemplates" [1]
"You may only test whether an attribute is present or absent, preserving 
separation of model
and view. The only exception is that if an attribute value is a Boolean object, it 
will test it for true/false."

I think you've hit a bug of StringTemplate transformer: see the attached patch (for cocoon-sample in C3 sources) using conditional and boolean value from sitemap; ST generator works well and ST transformer raises an exception similar to the one reported above.

So for me the exception either mean we do not support $if(condition)$ or that 
the compiler do not likes that $isUser$ is a String and not a boolean.

Are you sure that $isUser$ is a String? Shouldn't it be a boolean instead [3]?

TIA for any feedback!

salu2

[1] http://www.cs.usfca.edu/~parrt/course/601/lectures/stringtemplate.html
[2] http://old.nabble.com/-C3--Passing-parameters-from-sitemap-to-generator-td31971810.html [3] https://shiro.apache.org/static/current/apidocs/org/apache/shiro/subject/Subject.html#isAuthenticated%28%29

--
Francesco Chicchiriccò

Apache Cocoon Committer and PMC Member
http://people.apache.org/~ilgrosso/

Index: cocoon-sample/src/main/resources/COB-INF/string-template/template.xml
===================================================================
--- cocoon-sample/src/main/resources/COB-INF/string-template/template.xml	(revisione 1190180)
+++ cocoon-sample/src/main/resources/COB-INF/string-template/template.xml	(copia locale)
@@ -24,5 +24,11 @@
   <body>
     <h3>StringTemplate demo</h3>
     <p>parameter=$parameter$</p>
+    <p>booleanParameter=$booleanParameter$</p>
+$if(!booleanParameter)$
+<p>expression evaluated to true</p>
+$else$
+<p>expression evaluated to false</p>
+$endif$
   </body>
 </html>
Index: cocoon-sample/src/main/resources/COB-INF/sitemap.xmap
===================================================================
--- cocoon-sample/src/main/resources/COB-INF/sitemap.xmap	(revisione 1190180)
+++ cocoon-sample/src/main/resources/COB-INF/sitemap.xmap	(copia locale)
@@ -552,6 +552,7 @@
       <map:match wildcard="string-template/generator">
         <map:generate type="string-template" src="string-template/template.xml">
 	  <map:parameter name="parameter" value="A value"/>
+	  <map:parameter name="booleanParameter" value="{jexl:cocoon.request.isSecure()}"/>
         </map:generate>
         <map:serialize/>
       </map:match>
@@ -559,6 +560,7 @@
         <map:generate src="string-template/template.xml"/>
         <map:transform type="string-template">
 	  <map:parameter name="parameter" value="Another value"/>
+	  <map:parameter name="booleanParameter" value="{jexl:cocoon.request.isSecure()}"/>
         </map:transform>
         <map:serialize/>
       </map:match>

Reply via email to