10 years ago
4
Topic
Hi there,

I'm getting to grips with custom templating (don't have much php knowledge). So far I have found it quite easy to print the various fields in the template (using yootheme template with UI Kit).

I have run into a snag - which is probably only a basic bit of php but not sure on the correct syntax.

I have a youtube field which is optional, as such I want to only display the following code if the field exists for the particular article (i.e. if they have not added a video it will not be displayed);

<div>
<h3>Video Instructions</h3>
<?php
echo $cck->renderField('recipe_video');
?>
</div>

I assume it would be something with the php isset command but again, no idea on the syntax!

Any help is appreciated!

Thanks in advance
Simon
Get a Book for SEBLOD
175 Posts
webcastor
10 years ago
1
Level 1
The part echo $cck->renderField('recipe_video'); will only render if there is content, anyway.
If you want to test the value and not output the divs, maybe you can go with: if (strlen($cck->getValue('recipe_video')) > 0) { field display code block }
10 years ago
0
Level 2
That worked a charm as such;

<!-- Start of Video -->
<?php if (strlen($cck->getValue('recipe_video')) > 0) { ?>
<div>
<h3>Video Instructions</h3>
<?php
echo $cck->renderField('recipe_video');
?>
</div>
<?php } ?>
<!-- End of Video -->

Needs a little bit of cleaning up, using the Field name instead of hard coded text etc.

Thanks for your help
Simon
52 Posts
melihtas
10 years ago
1
Level 1
Hi,

strlen works with strings only, you can use !empty for all kinds of fields like field x.

<?php if (!empty($cck->get( 'your_field_name' )->value)): ?>

<!-- Your code -->

<?php endif; ?>


10 years ago
0
Level 2
Thanks for your input - that makes more sense and looks a bit cleaner too. I'll give that a shot.

Once I'm finished with my template I'll put up a post with a few snippets of code that helped me, hopefully will help some others too.

Thanks again
Get a VIP membership