pattern-lab / patternengine-php-twig

Twig-based PatternEngine for Pattern Lab.
http://patternlab.io/
MIT License
78 stars 36 forks source link

Clarify how listItems work with Twig #4

Closed RainbowArray closed 8 years ago

RainbowArray commented 9 years ago

I have been unable to find a way to generate a list of patterns as can be done in Mustache: http://patternlab.io/docs/data-listitems.html

When I have tried creating a listitems.json file for a particular pattern, like topic.listitems.json, I get an error like so:

configuring pattern lab... PHP Fatal error: Call to protected method PatternLab\Data::getListItems() from context 'PatternLab\PatternData\Rules\PatternInfoListItemsRule' in /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData/Rules/PatternInfoListItemsRule.php on line 54 PHP Stack trace: PHP 1. {main}() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:0 PHP 2. PatternLab\Console::run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:46 PHP 3. PatternLab\Console\Commands\GenerateCommand->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console.php:83 PHP 4. PatternLab\Generator->generate() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console/Commands/GenerateCommand.php:41 PHP 5. PatternLab\PatternData::gather() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Generator.php:69 PHP 6. PatternLab\PatternData\Rules\PatternInfoListItemsRule->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData.php:139

Fatal error: Call to protected method PatternLab\Data::getListItems() from context 'PatternLab\PatternData\Rules\PatternInfoListItemsRule' in /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData/Rules/PatternInfoListItemsRule.php on line 54

Call Stack: 0.0033 237016 1. {main}() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:0 0.0416 1139488 2. PatternLab\Console::run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:46 0.0467 1285960 3. PatternLab\Console\Commands\GenerateCommand->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console.php:83 0.0874 2896088 4. PatternLab\Generator->generate() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console/Commands/GenerateCommand.php:41 0.0925 3262920 5. PatternLab\PatternData::gather() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Generator.php:69 0.1008 3690656 6. PatternLab\PatternData\Rules\PatternInfoListItemsRule->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData.php:139

Warning: Command failed: PHP Fatal error: Call to protected method PatternLab\Data::getListItems() from context 'PatternLab\PatternData\Rules\PatternInfoListItemsRule' in /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData/Rules/PatternInfoListItemsRule.php on line 54 PHP Stack trace: PHP 1. {main}() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:0 PHP 2. PatternLab\Console::run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/core/console:46 PHP 3. PatternLab\Console\Commands\GenerateCommand->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console.php:83 PHP 4. PatternLab\Generator->generate() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Console/Commands/GenerateCommand.php:41 PHP 5. PatternLab\PatternData::gather() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/Generator.php:69 PHP 6. PatternLab\PatternData\Rules\PatternInfoListItemsRule->run() /Library/WebServer/Documents/sitename/docroot/sites/sitename/themes/themename/patternlab/vendor/pattern-lab/core/src/PatternLab/PatternData.php:139 Use --force to continue.

dmolsen commented 9 years ago

I'm now getting a slightly different "error" after fixing this. Really strange bug so it's going to take little bit to track it down. No rhyme or reason at the moment.

RainbowArray commented 9 years ago

Just curious, how would I iterate over the generic listItems file? I can get variables from data.json, but not clear on how to use listitems.json with twig.

dmolsen commented 9 years ago

This will all be squared away tonight. Messed up strpos argument order >.< To use listItems you'd do the following:

{% for listItem in listItems.four %}
    {{ listItem.title }}
{% endfor %}

That will give you a four item loop. I'm not 100% how you'd use includes with that just because of how Twig handles scoping.

RainbowArray commented 9 years ago

Hmmm. Tried that just now and wasn't able to get that syntax working, but maybe once you get a fix in that will get squared away. Thanks for looking at this Dave!

dmolsen commented 9 years ago

yeah. it definitely needs the fix.

On Tue, Apr 28, 2015 at 4:53 PM, Marc Drummond notifications@github.com wrote:

Hmmm. Tried that just now and wasn't able to get that syntax working, but maybe once you get a fix in that will get squared away. Thanks for looking at this Dave!

— Reply to this email directly or view it on GitHub https://github.com/pattern-lab/patternengine-php-twig/issues/4#issuecomment-97204048 .

http://dmolsen.com

dmolsen commented 9 years ago

Finally pushed the fix. Which edition are you using? I need to cut a new release if you're using the standard edition.

dave-cross commented 9 years ago

I see this one as still open, so I'm just gonna leave this here:

{# Twig has a `range` function #}
{% for listItem in 0..4 %}
    {{ listItem.title }}
{% endfor %}

Works for me.

dave-cross commented 9 years ago

Reviewing my code above, it's broken. But I did come up with another way using Faker.

Read about it over here: https://github.com/pattern-lab/edition-php-twig-standard/issues/7#issuecomment-115185589

Wylbur commented 7 years ago

Working with the twig version and wanted to use the listitems. I tried all these solutions, but nothing worked.

Is the proposed solution to use Faker instead?