Skip to content

Passing data to templates #29

Open
manolomartinez opened this Issue Aug 12, 2012 · 4 comments

2 participants

@manolomartinez

I'm starting to work with yst, and I am encountering an issue which may well be due to my lack of understanding. But, just in case: I'm trying to create a blog using yst; so far I'm trying to make this work:

index.yaml

- url      : index.html 
  title    : Hola
  template : index.st

- Blog:
  - url      : first-post.html
    title    : First Post
    data     : 
        title   : FROM first-post.yaml
        body    : FROM first-post.yaml
    template : blog.st

  - url      : second-post.html
    title    : Second Post
    data     : 
        title   : FROM second-post.yaml
        body    : FROM second-post.yaml
    template : blog.st

blog.st

# $title$

$body$

second-post.yaml

title: Test
body:   |

    Testing, testing, testing

For some reason, this combination of files does not populate blog.st with the title and text body in second-post.yaml. Instead, it does the following strange thing:

second-post.html

<html>
<head>
   <title>My Website - Second Post</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <link rel="stylesheet" type="text/css" href="css/screen.css" />
   <link rel="stylesheet" type="text/css" media="print" href="css/print.css" />
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript">    </script>
   <script src="js/nav.js" type="text/javascript"></script>
</head>
<body>
  <div id="doc3" class="yui-t1">
    <div id="yui-main">
      <div id="maincol" class="yui-b">
<h1 id="body">body: [</h1>
<p>Testing, testing, testing ]title: [Test]</p>
<p>body: [ Testing, testing, testing ]title: [Test]</p>
      </div>
    </div>
    <div id="nav" class="yui-b first">
<ul class="nav"
><li
  ><a href="index.html"
    >Hola</a
    ></li
  ><li class="active"
  ><a href="#"
    >Blog &#187;</a
    ><ul class="active"
    ><li
      ><a href="first-post.html"
    >First Post</a
    ></li
      ><li class="current"
      ><a href="second-post.html"
    >Second Post</a
    ></li
      ></ul
    ></li
  ></ul
>
    </div>
   </div>
</body>
</html>

What am I missing here? Or is this a bug? Thanks

@jgm
Owner
jgm commented Aug 19, 2012

Look at this part of index.yaml:

    data     : 
        title   : FROM first-post.yaml
        body    : FROM first-post.yaml

This tells yst to populate the both the title and the body template variables with the contents of first-post.yaml. Now, this yaml file contains a yaml object with two fields, title and body. So both title and body get assigned a yaml mapping with two fields.

You can fix this, I think, by doing the following in index.yaml:

    post     : 
        title   : FROM first-post.yaml
        body    : FROM first-post.yaml

Then, in your string template, use $post.title$ and $post.body$ where you now have $title$ and $body$.

@manolomartinez

Thank you. That doesn't seem to work. second-post.html is now:

<html>
<head>
   <title>My Website - Second Post</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <link rel="stylesheet" type="text/css" href="css/screen.css" />
   <link rel="stylesheet" type="text/css" media="print" href="css/print.css" />
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
   <script src="js/nav.js" type="text/javascript"></script>
</head>
<body>
  <div id="doc3" class="yui-t1">
    <div id="yui-main">
      <div id="maincol" class="yui-b">
<h1 id="section"></h1>
      </div>
    </div>
    <div id="nav" class="yui-b first">
<ul class="nav"
><li
  ><a href="index.html"
    >Hola</a
    ></li
  ><li class="active"
  ><a href="#"
    >Blog &#187;</a
    ><ul class="active"
    ><li
      ><a href="first-post.html"
    >First Post</a
    ></li
      ><li class="current"
      ><a href="second-post.html"
    >Second Post</a
    ></li
      ></ul
    ></li
  ></ul
>
    </div>
  </div>
</body>
</html>

That is, no body and no title. I've also tried:

data:
    post:
        title: FROM first-post.yaml
        body: FROM first-post.yaml

This prints the string "FROM first post.yaml" at both title and body.

@jgm
Owner
jgm commented Aug 23, 2012

Sorry, I didn't think it through. You're doing something a bit strange. Yst is designed to use yaml files like databases -- so, the files should be yaml lists of objects, not yaml objects as in your case.

Why don't you make each post a string template file, or just a plain markdon file? So, instead of first-post.yaml, you'd have first-post.txt.

# My first post

This is my first post.

index.yaml would then be:

- url      : index.html 
  title    : Hola
  template : index.st
- Blog:
  - url      : first-post.html
    title    : First Post
    source : first-post.txt
  - url      : second-post.html
    title    : Second Post
    source     : second-post.txt 
@manolomartinez

The main reason is that I was thinking of using a somewhat complicated template, with space for comments, and I thought it would be easier if I had some control over what I was passing to the different parts of the template, as opposed to just a block that includes title, date, body, etc.

I'll try to make it work in the way you suggest. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.