Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access to blog_posts collection from Meteor? #261

Open
urco opened this issue Apr 11, 2016 · 2 comments
Open

How to access to blog_posts collection from Meteor? #261

urco opened this issue Apr 11, 2016 · 2 comments

Comments

@urco
Copy link

urco commented Apr 11, 2016

Hi,
I would like to customize the templates for Index and for each blog post page. I have achieved the first goal (in the documentation there is a simple and great example to create a custom template for index layout). But I can't see any example to customize the blog post... How can I access from meteor to mongo collection (blog_posts collection) to get the data using the :slug ID? For example something like collection.findOne({slug:this.params.slug});) ?

Thanks in advance!

@hellogerard
Copy link
Contributor

The full blog_posts collection is not published on an individual blog post page because it's not necessary to send all that data. What are you trying to do?

@urco
Copy link
Author

urco commented Apr 16, 2016

Hi @hellogerard !
Ok, I will try to expose my problem, and I hope that´s become more clear.

In the package documentation, there is an example to customize the blog index template.

<template name="myBlogIndexTemplate">
  <h1>Welcome to my Blog</h1>
  <ul>
    {{#if blogReady}}
      {{#each posts}}
        <li>
          <h2>{{title}}</h2>
          <p>Published on {{publishedAt}}</p>
          <p>Excerpt: {{excerpt}}</p>
        </li>
      {{else}}
        <li>No posts found.</li>
      {{/each}}
    {{else}}
      <li>Loading...</li>
    {{/if}}
  </ul>
</template>

I have customize following the previous example my index blog and it works fine. I have changed the line <h2>{{title}}</h2> for<a href="/{{slug}}"><h2>{{title}}</h2></a> in my custom template, but the link ({{title}} is not working (it is not linked to the post). My question is, how could I customize my blog index, and keep the link with the post?. Right now, if I click the link, nothing happen (blank page). I post my code below if it can help you to understand my problem.

*In the client * layoutIndexBlog.html

<template name="layoutIndexBlog">
   <!--{{> blogIndex}}-->
  <div class="container">
    <div class="row">
        {{#if blogReady}}
              {{#each posts}}
                <li>
                  <a href="/{{slug}}"><h2>{{title}}</h2></a>
                  <p>Published on {{publishedAt}}</p>
                  <img src="{{featuredImage}}" class="img-responsive"> 
                  <p>{{excerpt}}</p>
                    {{> shareit}}
                </li>
              {{else}}
                <li>No posts found.</li>
              {{/each}}
            {{else}}
              <li>Loading...</li>
        {{/if}}
    </div>    
</div>
</template>

layoutBlog.html

<template name="layoutBlog">
    <div class="container">
        <!--{{> blogShow}}-->
        <h2>{{title}}</h2>
        <p>{{body}}</p>
    </div>
</template>

layoutPost.html

template name="layoutPost">
    {{> yield "header"}}
    <div class="container">
        {{> yield "layoutBlog"}}
    </div>
</template>

*In /lib folder *

Blog.config({

     dateFormat: 'MMM Do, YYYY',

     adminRole: 'blogAdmin',
     authorRole: 'blogAuthor',

     blogIndexTemplate: 'layoutIndexBlog', // '/blog' route
     blogShowTemplate: 'layoutPost',    // '/blog/:slug' route*/
     //blogLayoutTemplate:'layoutPost',
     basePath: '/', 
     title: "Portfolio Website",

      rss: {
        title: 'My blog',
        description: 'blog Javi'
      }
});

route.js

Router.route('/', function () {
  this.layout('layoutHome');

   this.render('header', {
    to:"header"
  });

   this.render('layoutIndexBlog', {
        to:"layoutIndexBlog"
   });
});
/*
Router.route('/:slug', function () {
  this.layout('layoutPost');

   this.render('header', {
    to:"header"
  });

  this.render('layoutBlog', {
    to: "layoutBlog"
  });
});*/ 

Router.route('/users', function () {
  this.layout('layoutUsers');

   this.render('header', {
    to:"header"
  });

   this.render('users', {
        to:"users"
   });
});

The comented code in route.js file is the tricky part... I can get the slug paramenter with .params but I don´t know how can I keep the relationship between the post entry in the index and the post page... I don´t know how could make a .findOne query to blog_posts collection to return the right blog post page using the :slug parameter....

Thanks in advance, and congratulations for your work with this package! It is amazing! I wait your suggestion ( Save my life man!!;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants