1

I am making an ajax request to an entry. It is returning the entry's html - and all is working great.

I have a section called "portfolio" and I loop through each:

portfolio/index.twig

{% set projects = craft.entries.section('portfolio') %}
...
{% for project in projects %}   
    ...
    <a href="#!{{ project.slug }}" class="hover-overlay">
    ...
{% endfor %}

When the project is clicked, I am making an ajax request to an entry. It is returning the entry's html - and all is working great.

Now I would like to strip out the static html, and access the parent's entry variable.

For example, here is the html that I am returning.

portfolio/_entry.twig

<div class="item-content">
    <div class="container">
        <h3>My Project</h3>
        <div class="row">
        <div class="col-md-9">
            {# image will go here. #}
        </div>
        <div class="col-md-3">
            <h4>My Project Description</h4>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas eros dolor, efficitur lobortis eros et, dictum hendrerit nulla.</p>
            <p>Client: My Client</p>
        </div>
    </div>
</div>

What I would like to do is:

...
<h3>{{ project.title }}</h3>

I'm not sure how to do is something like this from within portfolio/_entry.twig

{% set project = ?? %}

So that I can use {{ project.title }}

Thank you for any suggestions!

  • You know you can pass variables into templates you render in your controller. So you can just send the ID of your project with your ajax request (or the slug) and fetch the project by id (or slug) in your controller, then you pass it into the template. Your 2. Parameter of renderTemplate is an array of all variables you like to access in your template. I'm not sure where the problem is – Robin Schambach Nov 1 '17 at 21:21
1

Since you are only trying to access the existing entry model, you should be able to do something like this:

portfolio/_entry.twig

{% set project = entry %}
...
<h1>{{ project.title }}</h1>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.