Ralf Posted March 9, 2014 Share Posted March 9, 2014 Hi all, somewhat since my last visit here in the forum but now I have something time and many pages that I would like to realize with ProcessWire ago. Let's restart with a simple question probably for you. In the past I used the CMS Conpresso and there in the template, it was possible to examine "Fields" on content. For example: <div class="content"> <div><!-- DROPDOWN: Event; --><!-- ifNotSet: Event; --><!-- Text: Event_alternative; --><!-- /ifNotSet --></div> <div><!-- TEXT: Datum; format=D j.m.; --></div> <!-- ifSet: Homepage; --><div><a href="http://<!-- URL: Homepage; -->" target="_new"><!-- Text: Homepage; --></div><!-- /ifSet --> <!-- ifSet: Departure_AUI; --><div>Aui: <!-- TEXT: Departure_AUI; --></div><!-- /ifSet --> <!-- ifSet: Departure_MAG; --><div>Mag: <!-- TEXT: Departure_MAG; --></div><!-- /ifSet --> </div> My problem now is that I'm not sure how do I convert this in HTML/PHP? - Has ProcessWire its own solution for this task setting? - Can I solve the problem with an "if-clause"? - someting else... Sorry for this confusion but could someone help me on the jumps with this problem? I know, the handling of the frontends in ProcessWire is free but I'm not soo familiar with php and actual I have no own "style". Many thanks Ralf Link to comment Share on other sites More sharing options...
adrian Posted March 9, 2014 Share Posted March 9, 2014 Hi Ralf, If I understand correctly, it should be a simple matter of: if($page->Departure_AUI){ echo "<div>Aui: {$page->Departure_AUI}</div>"; } There are shorter/cleaner ways of doing this, but I think this is the most understandable for someone new to php. Hope that helps! 2 Link to comment Share on other sites More sharing options...
pwired Posted March 9, 2014 Share Posted March 9, 2014 http://processwire.com/api/variables/fields/ Example to check if an image(s) field has something in it: if(count($page->images)) { // the page has one or more images } $image = $page->images->first();if($image) do something; Link to comment Share on other sites More sharing options...
Ralf Posted March 10, 2014 Author Share Posted March 10, 2014 @ adrian thanks for your fast answer!! And you´re right you understood me correctly and i´m new at php respectively i can read a little bit I will try the code asap and if i have further questions, i´ll come back... Purely out of interest, how does a "better / faster" code look like? i would happy to learn something new. @ pwired also like to thank. When i have the situation with pictures i´ll come back to your code. Link to comment Share on other sites More sharing options...
kongondo Posted March 10, 2014 Share Posted March 10, 2014 .....Purely out of interest, how does a "better / faster" code look like? i would happy to learn something new. Like those shown here... http://processwire.com/talk/topic/5640-shortest-way-to-echo-a-field-only-if-populated/ 2 Link to comment Share on other sites More sharing options...
horst Posted March 10, 2014 Share Posted March 10, 2014 (edited) Purely out of interest, how does a "better / faster" code look like? i would happy to learn something new. alternatives: http://processwire.com/talk/topic/5640-shortest-way-to-echo-a-field-only-if-populated/ But if I were new to PHP I would write a few chars more and have better readability. EDIT: damn, Kongondo! Edited March 10, 2014 by horst 3 Link to comment Share on other sites More sharing options...
kongondo Posted March 10, 2014 Share Posted March 10, 2014 Sorry @Horst, I beat you to it 1 Link to comment Share on other sites More sharing options...
adrian Posted March 10, 2014 Share Posted March 10, 2014 I tend to agree with horst on this, but it can make life a lot easier if you learn some basic implementations of ternary operators, depending on how you want to structure your html and php. This can be easier: echo $page->Departure_AUI ? "<div>Aui: {$page->Departure_AUI}</div>" : ""; In my mind it really comes down to whether that div can be left empty, or if it must not be echo'd at all, which depends on your layout. It can also be easier sometimes to build up sections of content in a variable first. $out = ''; $out .= $page->Departure_AUI ? "<div>Aui:{$page->Departure_AUI}</div>" : ""; $out .= $page->Departure_MAG ? "<div>Mag: {$page->Departure_MAG}</div>" : ""; echo "<div id="block-parent">$out</div>"; 3 Link to comment Share on other sites More sharing options...
Ralf Posted March 15, 2014 Author Share Posted March 15, 2014 echo "<div id="block-parent">$out</div>"; Thanks adrian, but i changed your last line with the following code that it works like a charm echo "<div id=\"block-parent\">" . $out . "</div>"; 1 Link to comment Share on other sites More sharing options...
adrian Posted March 15, 2014 Share Posted March 15, 2014 Thanks for fixing my sloppy code Also, here is a great article on ternary operators: http://davidwalsh.name/php-shorthand-if-else-ternary-operators Anything you think you might be sacrificing in PW by not using templating syntax, is more than made up by the abilities of simple everyday PHP. Ryan also wrote a good article here: http://processwire.com/api/why-php-syntax/ 2 Link to comment Share on other sites More sharing options...
Ralf Posted March 16, 2014 Author Share Posted March 16, 2014 @ adrian THANK you very much!! This two articles are great... and with this sentence Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if/else structures. and your code example echo $page->Departure_AUI ? "<div>Aui: {$page->Departure_AUI}</div>" : "";made it "click" in my head and again I learned something important, now I have to do only the "cheatsheet" and finally understand it myself, this would certainly dissolve another big knot in my head ... but one step after the other 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now