开发者

Changing <title> with Lift

Is it possible to dynamically switch the title of a page that is served by Lift without having to write an extra snippet for that particular case?

One option is of course &开发者_开发技巧lt;lift:mySnippet><title>Default Title</title></lift:mySnippet> but I thought there might be an option along the lines of <head_merge><title>New Title</title></head_merge> (which inserts a second title node).

I do not like the first approach since I do not want to stick all the title generation logic into a single snippet and ask what kind of page I am on etc.


Have you tried to use templates?

You can define template in templates-hidden/default.html like this:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">

    <head>
        <title>
            <lift:bind name="title" />
        </title>

        ...
    </head>

    <body>
        <lift:bind name="content" />
    </body>
</html> 

And use it in index.html for example:

<lift:surround with="default">
    <lift:bind-at name="title">Home</lift:bind-at>
    <lift:bind-at name="content">
        my content
    </lift:bind-at>
</lift:surround>

You can find more information about templates here:

http://www.assembla.com/spaces/liftweb/wiki?id=liftweb&wiki_id=Templates_and_Binding


One way is to use the Menu.title snippet.

In bootstrap/liftweb/Boot.scala you define the sitemap with page names:

class Boot {
  def boot {
    // ...
    def sitemap = SiteMap(
      Menu.i("Home") / "index",
      Menu.i("About") / "about")
    // ...
  }
}

In templates-hidden/default.html you use the snippet:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
<head>
  ...
  <title class="lift:Menu.title">AppName:</title>
  ...

Then page titles will be: "AppName: Home" and "AppName: About". This is nice if you use

<span class="lift:Menu.builder"></span>

to build the menu, because page titles will be the same used in the menu.

Another approach is to use head merge and define the title in the page's html. For this to work, you have to remove the <title> tag from templates-hidden/default.html and put an <head> or <head_merge> tag in your content block:

<!DOCTYPE html>
<html>
  <body class="lift:content_id=main">
    <div id="main" class="lift:surround?with=default;at=content">
    <head_merge>
      <title>TITLE OF THIS PAGE HERE</title>
    </head_merge>
    ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜