Jump to content

Twig variable


taotoo
 Share

Recommended Posts

I'm a beginner and was wondering why the code below isn't working.

The issue is where ARTIST (capitalised) appears in the code for the second time. I want it to take the value from the first mention of ARTIST, but it's not doing anything. If I replace the second instance with the name of an artist, then it displays as expected.

Hopefully I'm making a basic error!

<!--Show artists that are related to current exhibition-->
{% set ARTIST = page.exhibition_artist %}
	{% for artist in artist %}
		{{ artist.title }}
			<!--Show works that are related to current exhibition and belong to a given artist-->
			{% set work = pages.find("template=work, work_exhibition=[page.path], work_artist=[ARTIST]") %}
				{% for work in work %}
					{{ work.title }}
				{% endfor %}
	{% endfor %}

 

Link to comment
Share on other sites

As said before, I don't know Twig that much, but with PHP it's not that much uglier. PHP is a template language in itself, imho. Here's the equivalent (well, it should work ;-):

<?php foreach ($page->exhibition_artist as $artist): ?>
    <?=$artist->title;?>
    <!-- Show works that are related to current exhibition and belong to a given artist -->
    <?php foreach ($pages->find("template=work, work_exhibition=$page->path, work_artist=$artist") as $work): ?>
        <?=$work->title;?>
    <?php endforeach;?>
<?php endforeach;?>

 

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

12 minutes ago, titanium said:

I'm no Twig expert at all, but I think the foreach loop should look like this:


{% set ARTISTS = page.exhibition_artist %}
	{% for artist in artists %}

Maybe it would be easier for you if you do it with plain PHP, without Twig - for me it is :-)

Thanks - unfortunately it's still not working though. I actually managed to do it in PHP, but I thought Twig might be easier to learn. This works fine:

	<?php
		foreach($page->exhibition_artist as $a) {
		echo "$a->title";
		/*Get works whose work_exhibition field points to the current last segment and work_artist field points to the given artist*/
			$wor = $pages->find("template=work, work_exhibition=[{$page->path}], work_artist=[$a]");
			/*Render works*/
			foreach ($wor as $w) {
				foreach($w->work_image as $image) {
				echo "&nbsp;&nbsp;<img src='$image->url' width='200'>";
				}
			/*Render artists*/
			echo "{$w->title}";
			}
		}
	?>

 

Link to comment
Share on other sites

5 minutes ago, titanium said:

As said before, I don't know Twig that much, but with PHP it's not that much uglier. PHP is a template language in itself, imho. Here's the equivalent (well, it should work ;-):


<?php foreach ($page->exhibition_artist as $artist): ?>
    <?=$artist->title;?>
    <!-- Show works that are related to current exhibition and belong to a given artist -->
    <?php foreach ($pages->find("template=work, work_exhibition=$page->path, work_artist=$artist") as $work): ?>
        <?=$work->title;?>
    <?php endforeach;?>
<?php endforeach;?>

 

Thanks for this - looks neater than what I came up with - I'm sure I can learn from it.

Link to comment
Share on other sites

Interestingly, if I remove ARTIST from the the pages.find line, and instead put it in a conditional, it then works fine.

<!--Show artists that are related to current exhibition-->
{% set ARTIST = page.exhibition_artist %}
	{% for artist in artist %}
		{{ artist.title }}
			<!--Show works that are related to current exhibition and belong to a given artist-->
			{% set work = pages.find("template=work, work_exhibition=[page.path]") %}
				{% if work.work_artist==ARTIST %}
					{{ work.title }}
				{% endif %}
	{% endfor %}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...