Ok, this one should be simple for someone who uses it, but drives me insane at the moment. I want to know what action is the current one in my Element. i could use #{request} but i’m confident there is something more simple, and i just cannot find it in any of the examples… i want to make a menu like:
<?r if location =~ /forum/ ?> #{content :forumbar} <?r elsif location =~ /gallery/ ?> #{content :gallerybar} <?r end ?>
Hope that makes it somewhat clear… i need this one for a much more complicated structure, but it should be sufficent if i just know what the current action is without storing it into sessions :|
(4 attempts)
Kashia answered:
This is just a question on how and when something gets escaped in the Nitro pipeline.
Please note that do not get escaped inside of an element.
class MyEle < Nitro::Element def render %~ <?r if request.path == "/forum" ?> #{content :forumbar} <?r elsif request.path == "/gallery" ?> #{content :gallerybar} <?r end ?> ~ end end
This will evaluate to the following code, when it gets rendered in the Template:
<?r if request.path =~ /^/forum/ ?> <!-- the html code from the forum bar --> <?r elsif request.path =~ /^/gallery/ ?> <!-- the html code from the gallery bar --> <?r end ?>
So, the do not get escaped in the Element, just normal #{} do. If you want to use code, which has to evaluate in the Template, escape the #.
%~
#{@request.inspect}
~
Please also note, that if you use =~ inside the of the %~ string ~, you have to escape the ~.
def render %~ <?r if @request.path =~ / orum/ ?> ~ end
Fabian answered:
You can use
<?r if @context.headers['REQUEST_URI'] =~ /forum/ ?> #{content :forumbar} <?r elsif @context.headers['REQUEST_URI'] =~ /gallery/ ?> #{content :gallerybar} <?r end ?>
or
<?r if @request.path =~ /forum/ ?> #{content :forumbar} <?r elsif @request.path =~ /gallery/ ?> #{content :gallerybar} <?r end ?>
@request.path gets the path. In your case it'd return "/forum" or "/gallery".
bsoto answered:
I haven't tested, but there is an instance variable available in the page named @action_name.
Perhaps changing your element to:
<Element location="#{@action_name}" ... > </Element>
would work?
zimbatm answered:
The best method IMHO works in two parts.
First you need to change the compiler pipeline so that the Morpher gets executed after the Element.
Nitro::Compiler.setup_template_transform do |template| template = Nitro::StaticInclude.transform(template, self) template = Nitro::Elements.transform(template, self) template = Nitro::Morphing.transform(template, self) template = Nitro::Markup.transform(template, self) template = Nitro::Localization.transform(template, self) template = Nitro::Cleanup.transform(template, self) template = Nitro::Template.transform(template, self) end
Then you can use the morphing syntax in your Element combined with the @action_name :
%{
<div if="@action_name == 'forum'">#{content :forum}</div>
<div id="@action_name == 'gallery'>#{content :gallery}</div>
}