Setting title in a template when it is not a String literal

11 views
Skip to first unread message

Harshdeep S Jawanda

unread,
Apr 26, 2014, 9:23:21 AM4/26/14
to Rythm Engine
​How do I set the title of a page when that value has be retrieved from a parameter passed to the Rythm template?

I have a base template and a template that extends it. In the extending template I have to use the value from a parameter to set the title of the page. So, for example:
@extends(base, title: @aParameter.getName() )

but the above code doesn't seem to work. Also, this doesn't let me check whether aParameter is non-null. Is there a way I can set the value of the title parameter later in the body (since @set is now deprecated)?

Thanks.

Harshdeep S Jawanda

unread,
Apr 26, 2014, 11:09:28 AM4/26/14
to Rythm Engine
So I found out the answer by hit-and-trial:
@extends(base, title: aParameter.getName() )

Just had to leave out the "@" near the beginning.

Please note: in my case the @import and @args statements precede the @extends() statement.
--
Regards,
Harshdeep S Jawanda

Harshdeep S Jawanda

unread,
Apr 28, 2014, 3:46:26 AM4/28/14
to Rythm Engine
Please note: I still have not found a way to check whether aParameter is non-null before I call a method on it to get the value of the page title. Any suggestions?

In the docs I've only seen so far:
@if (aParameter) {aParameter.getName()} else {""}
etc., but that doesn't work when passing an argument to a base template that I am extending.

Harshdeep S Jawanda

unread,
Apr 28, 2014, 4:03:33 AM4/28/14
to Rythm Engine
A better way to put this --- how do I achieve the following in current Rythm code:
@args Parameter aParameter
@extends(base)
@if (aParameter) {
    @set("title" : aParameter.getName())
} else {
    @set("title" : "Default title")
}

Roman Bykovskiy

unread,
Apr 28, 2014, 4:04:08 AM4/28/14
to Rythm Engine, Harshdeep S Jawanda
Maybe it’s because your code do nothing. 
If you need to render the value of Parameter.getName() - you should add @ sign before:

@if(aParameter){
  @aParameter.getName()


-- 
Roman Bykovskiy

На 28 апреля 2014 г. в 11:46:58, Harshdeep S Jawanda (hsja...@gmail.com) написал:

--
You received this message because you are subscribed to the Google Groups "rythmengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rythmengine...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

green

unread,
Apr 28, 2014, 4:24:07 AM4/28/14
to Harshdeep S Jawanda, Rythm Engine
You can't set to parent template fields at the time the template is rendering. Use @get() and @set(). Checkout http://fiddle.rythmengine.org/#/editor/45d2594b867749519fc88aa4af722d93




--

Harshdeep S Jawanda

unread,
Apr 28, 2014, 4:24:45 AM4/28/14
to Rythm Engine
Even if I write:

@extends(base, title: @if (aParameter) {@aParameter.getName()} else {"No such article"})

I still don't get any output.

Harshdeep S Jawanda

unread,
Apr 28, 2014, 4:27:58 AM4/28/14
to Rythm Engine
Yes, I had verified that @set works... but then I get deprecated warnings. Was trying to avoid that.

Roman Bykovskiy

unread,
Apr 28, 2014, 4:29:05 AM4/28/14
to Rythm Engine, Harshdeep S Jawanda
If you trying not to render but pass param you shouldn’t use any of @ 

So if you pass params - it’s just a typical java way:

@extends(base, title: if(aParameter!=null){aParameter.getName()}else {"No such article"})

If you check it in render:

template:

@extends(base, title: aParameter)

base wrapper:

@if(aParameter){
  @aParameter.getName()
}



-- 
Roman Bykovskiy

На 28 апреля 2014 г. в 12:25:16, Harshdeep S Jawanda (hsja...@gmail.com) написал:

Roman Bykovskiy

unread,
Apr 28, 2014, 4:35:46 AM4/28/14
to Rythm Engine, Harshdeep S Jawanda
i’m sorry, for the first one, the correct java way: 

@extends(base, title: aParameter.getName()!=null?aParameter.getName:"No such article")

also you can redefine your getter in a aParameter class:

public String getName(){
  if(this.name==null) return "No such article"
  return this.name
}


-- 
Roman Bykovskiy

На 28 апреля 2014 г. в 12:29:07, Roman Bykovskiy (802...@gmail.com) написал:

Harshdeep S Jawanda

unread,
Apr 28, 2014, 4:55:37 AM4/28/14
to Rythm Engine
I tried this and it resulted in a syntax error: Syntax error on token "if", invalid Expression

I cannot check aParameter for null in the base template as not all extending templates get an object of type aParameter. Yes, I could use the value of aParameter.toString() but I don't want to do that, semantically it seems too generic and an abuse of the toString method. Plus, depending on the object, Object.toString() may not be what I want to show.

The 2nd method you suggested doesn't work either (if written as: @extends(base, title: null != aParameter ? aParameter.getName() : "No such article")

If I write @extends(base, title: (null != aParameter) ? aParameter.getName() : "No such article") , then an error is generated ("java.lang.Boolean cannot be cast to java.lang.String") --- the only difference being putting a pair of parentheses around null != aParameter.

As far as I can make out, using @set is the only way that works (as Gelin suggested).

Roman: my concern is not that the field name inside aParameter might be null (that's already handled), my concern is that aParameter itself might be null. So doing a null check on this.name doesn't work --- the NullPointerException would have already happened.

Thanks everybody for your help.

Roman Bykovskiy

unread,
Apr 28, 2014, 5:28:57 AM4/28/14
to Harshdeep S Jawanda, Rythm Engine
That’s odd… It works for me.
Anyway, one more way: you can just define new variable in template

@{
  String myTitle = "No such article";
  if(aParameter!=null && aParameter.title!=null)
    myTitle = aParameter.getName();
}

@extends(base,title: myTitle);


-- 
Roman Bykovskiy

На 28 апреля 2014 г. в 12:56:07, Harshdeep S Jawanda (hsja...@gmail.com) написал:

Harshdeep S Jawanda

unread,
Apr 28, 2014, 7:00:20 AM4/28/14
to Roman Bykovskiy, Rythm Engine
Yeah... already tried that... isn't working for me...

I am using Rythm 1.0.1-SNAPSHOT. Which version are you using?

Harshdeep S Jawanda

unread,
Apr 28, 2014, 7:06:35 AM4/28/14
to Rythm Engine
I am wondering if I am using the correct version of Rythm...

In my pom.xml I have:

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>7</version>
    </parent>

    ...

        <dependency>
            <groupId>org.rythmengine</groupId>
            <artifactId>rythm-engine</artifactId>
             <version>1.0.1-SNAPSHOT</version>
        </dependency>


Is this correct?

Other configuration (just in case it makes any difference):
  • GAE SDK 1.8.8
  • Eclipse Kepler
  • Mac OS 10.9.2

green

unread,
Apr 28, 2014, 7:11:00 AM4/28/14
to Rythm Engine
Your pom.xml is correct

Roman Bykovskiy

unread,
Apr 28, 2014, 7:40:52 AM4/28/14
to Harshdeep S Jawanda, Rythm Engine
Maybe the problem in your object?
Just try to render your object in template, render this new var in template. 

— 
Roman Bykovskiy

На 28 апреля 2014 г. в 15:00:50, Harshdeep S Jawanda (hsja...@gmail.com) написал:

Harshdeep S Jawanda

unread,
Apr 28, 2014, 7:48:32 AM4/28/14
to Roman Bykovskiy, Rythm Engine
Other than setting the page title (which is of course defined in the base template), the object and its various properties work fine in the extending template.
Reply all
Reply to author
Forward
0 new messages