Skip to main content
added 3281 characters in body
Source Link
Phil
  • 250
  • 1
  • 5

Further Info

I have tried the solution from Edit 2 and still having an issue. Perhaps a little more info. There is a template _layout as is common. The template I am accessing is called index.html and is the homepage for the site. It includes

{% extends _layout %}
{% block headercontent %}
  code that prints out featured article
{% endblock %}
{% block content %}
  code that prints out remainder of articles minus feature
{% endblock %}

Trying to implement your Edit 2 solution I tried just declaring the variable in index.html outside of the blocks. That didn't work so I tried declaring it in _layout before the block calls. That also didn't work so then I tried in _layout {% set homeFeature = "foo" %} and in index.html {% set homeFeature = "bar" %} Then I added a print out of the {{ homeFeature }} variable within both blocks. Within the headercontent block i get 13 (the entry's id) and within the content block i get "foo" which I find very odd. Here is the full code minus some html if it helps.

_layout

 {% set homeFeature = "foo" %}
 <header>
 some other html
 {% block headercontent %}
 {% endblock %}
 </header>
 {% block content %}
 {% endblock %}

index.html

{% extends "_layout" %}
{% set homeFeature = "bar" %}
{% block headercontent %}
  <div id="home-featured">
    {% set entries = craft.entries({
      section: 'articles',
      limit: 1,
      search: 'featuredOnHomepage:1'
    }) %}
    {% for entry in entries %}
      {% set homeFeature = entry.id %}
      <h1>The feature id is: {{ homeFeature }}</h1>
      <h4>Featured</h4>
      <h2><a href="/articles/{entry.url}">{{ entry.title }}</a></h2>
      {{ entry.articleSummary }}
      <p>FILED IN: 
      {% set categories = craft.categories({
        relatedTo: { sourceElement: entry, field: "typeCategory, mainCategory" },
        limit:     null
      }) %}
      {% for category in categories %}
        <a href="{{ category.url }}">{{ category.title }}{% if not loop.last %}, {% endif %}</a>
      {% endfor %}
      </p>
    {% endfor %}
  </div>
{% endblock %}

{% block content %}
  <main id="content" role="main">
    <div class="green-block">
      <div id="home-stamp" class="wrapper">
        <img src="/css/gfx/home-map.gif" />
        {{ entry.body }}
      </div>
    </div>
    <div class="wrapper">
      {% set entries = craft.entries({
        section:        'articles',
        order:          'postDate',
        limit:          4,
        id: 'not '~homeFeature
      }) 
      %}
      {% for entry in entries %}
        <h1>The feature id is: {{ homeFeature }}</h1>
        <div class="summary-box">
          <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>  
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
            <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
          {% endif %}
        </div>
      {% endfor %} 
    </div>
  </main>
{% endblock %}

Further Info

I have tried the solution from Edit 2 and still having an issue. Perhaps a little more info. There is a template _layout as is common. The template I am accessing is called index.html and is the homepage for the site. It includes

{% extends _layout %}
{% block headercontent %}
  code that prints out featured article
{% endblock %}
{% block content %}
  code that prints out remainder of articles minus feature
{% endblock %}

Trying to implement your Edit 2 solution I tried just declaring the variable in index.html outside of the blocks. That didn't work so I tried declaring it in _layout before the block calls. That also didn't work so then I tried in _layout {% set homeFeature = "foo" %} and in index.html {% set homeFeature = "bar" %} Then I added a print out of the {{ homeFeature }} variable within both blocks. Within the headercontent block i get 13 (the entry's id) and within the content block i get "foo" which I find very odd. Here is the full code minus some html if it helps.

_layout

 {% set homeFeature = "foo" %}
 <header>
 some other html
 {% block headercontent %}
 {% endblock %}
 </header>
 {% block content %}
 {% endblock %}

index.html

{% extends "_layout" %}
{% set homeFeature = "bar" %}
{% block headercontent %}
  <div id="home-featured">
    {% set entries = craft.entries({
      section: 'articles',
      limit: 1,
      search: 'featuredOnHomepage:1'
    }) %}
    {% for entry in entries %}
      {% set homeFeature = entry.id %}
      <h1>The feature id is: {{ homeFeature }}</h1>
      <h4>Featured</h4>
      <h2><a href="/articles/{entry.url}">{{ entry.title }}</a></h2>
      {{ entry.articleSummary }}
      <p>FILED IN: 
      {% set categories = craft.categories({
        relatedTo: { sourceElement: entry, field: "typeCategory, mainCategory" },
        limit:     null
      }) %}
      {% for category in categories %}
        <a href="{{ category.url }}">{{ category.title }}{% if not loop.last %}, {% endif %}</a>
      {% endfor %}
      </p>
    {% endfor %}
  </div>
{% endblock %}

{% block content %}
  <main id="content" role="main">
    <div class="green-block">
      <div id="home-stamp" class="wrapper">
        <img src="/css/gfx/home-map.gif" />
        {{ entry.body }}
      </div>
    </div>
    <div class="wrapper">
      {% set entries = craft.entries({
        section:        'articles',
        order:          'postDate',
        limit:          4,
        id: 'not '~homeFeature
      }) 
      %}
      {% for entry in entries %}
        <h1>The feature id is: {{ homeFeature }}</h1>
        <div class="summary-box">
          <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>  
          {% set asset = entry.articleImage.first() %}
          {% if asset %}
            <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
          {% endif %}
        </div>
      {% endfor %} 
    </div>
  </main>
{% endblock %}
Source Link
Phil
  • 250
  • 1
  • 5

Test a variable in a set entries tag

I have a page with a featured entry at the top and then remaining entries showing below. On the first

{% set entries = craft.entries({
  section: 'articles',
  limit: 1,
  search: 'featuredOnHomepage:1'
}) %}
{% for entry in entries %}
  entry print out
  {% set homeFeature = entry.id %}
{% endfor}

Later in the template I do

    {% set entries = craft.entries({
        section:        'articles',
        order:          'postDate',
        limit:          4
    }).id('not ' ~ homeFeature) 
    %}
    {% for entry in entries %}
      <div class="summary-box">
        <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>    
        {% set asset = entry.articleImage.first() %}
        {% if asset %}
          <a href="{{ entry.url }}"><img src="{{ asset.getUrl('summaryImage') }}" /></a>
        {% endif %}
      </div>
    {% endfor %} 

But it is still including the feature id. If I hardcode in the entry id it works fine. I have tried to print the {{ homeFeature }} variable in the second part and I get nothing yet in the first part it prints out the correct ID. I am assuming the variables don't persist through the different loops. Is there a way to make them persist or write the code differently?