Default value for a page variable

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Default value for a page variable

David Hladky
Hi,

I am trying to find some way how to use a default value for a variable in generated page. Let us say this is the content of the index.html.haml page:

---
layout: base
title: Some page title
---

%h1 You're awestruct.

---------------------------------------------------------------------------------------------------------
And let us say in base.html.haml there is

%title
  #{page.title}

In this case the output page will render with the title "Some page title". However if I remove the title: line from the index file, I would like to have let us say "Title is undefined" rendered instead of the empty value. Can someone give me any hint how to do this? I spent several hours trying to google something out, but so far with no luck.

Thank you,

David Hladky
Reply | Threaded
Open this post in threaded view
|

Re: Default value for a page variable

bobmcwhirter
Administrator
Define the default value in your layout might work.

Bob

--
Sent from my iPhone


On Oct 24, 2012, at 7:37 AM, David Hladky <[hidden email]> wrote:

> Hi,
>
> I am trying to find some way how to use a default value for a variable in
> generated page. Let us say this is the content of the index.html.haml page:
>
> ---
> layout: base
> title: Some page title
> ---
>
> %h1 You're awestruct.
>
> ---------------------------------------------------------------------------------------------------------
> And let us say in base.html.haml there is
>
> %title
>  #{page.title}
>
> In this case the output page will render with the title "Some page title".
> However if I remove the title: line from the index file, I would like to
> have let us say "Title is undefined" rendered instead of the empty value.
> Can someone give me any hint how to do this? I spent several hours trying to
> google something out, but so far with no luck.
>
> Thank you,
>
> David Hladky
>
>
>
> --
> View this message in context: http://talk-archive.awestruct.org/Default-value-for-a-page-variable-tp64.html
> Sent from the Awestruct Talk mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> Archives: http://talk-archive.awestruct.org/
> To unsubscribe, e-mail: [hidden email]
>

---------------------------------------------------------------------
Archives: http://talk-archive.awestruct.org/
To unsubscribe, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Default value for a page variable

MonKe
In reply to this post by David Hladky
I did not test this solution, but wouldn't a bit of logic work just fine here ?

example :

%title= page.title || site.defaults.title

dunno if it works fine or not, given I am not an experienced rubyist, but looks fine from far away.
___________________________

Kevin Thommy
aka MonKe
Int├ęGraphiste @ Clever Cloud

G+ Tw Tr
Reply | Threaded
Open this post in threaded view
|

Re: Default value for a page variable

David Hladky
Thank you very much for your help. It works.

I am completely new to Ruby. So if understand the logic of the || operator you used, does that mean, that it returns the first non-nil value as the result (or nil if both are nils) and it is a standard thing? I found few pages, where the Ruby operands are described, but I would never expected such behaviour.

MonKe wrote
I did not test this solution, but wouldn't a bit of logic work just fine
here ?

example :

%title= page.title || site.defaults.title

dunno if it works fine or not, given I am not an experienced rubyist, but
looks fine from far away.
Reply | Threaded
Open this post in threaded view
|

Re: Default value for a page variable

MonKe
It is more of a shortcut than a regular behavior.

The logical OR ( || ) operator tends, in some languages, to interpret empty and null values as false, and non-empty or non-null as true. Looks like it is the case in Ruby.

You probably have many other ways to write this, using ternary operators, if / else, etc. but this is short and readable, so it works for me.
___________________________

Kevin Thommy
aka MonKe
Int├ęGraphiste @ Clever Cloud

G+ Tw Tr