Jump to content
Lmwt

Targetting region and display fields names

Recommended Posts

Hello,

and one more beginner question: I am using the multi-language site profile and having troubles targeting the region I want to populate in my templates due to delayed output. The list is now appearing on top of the nav bar 🙂 and the values of the fields on one line down the title. I want these fields to be displayed in a list underneath the title of the page, and I also would like the name of the field to be displayed as a string in front of the value... I dont know how to do this. Can someone help?

right now my code looks like this: 

<?php namespace ProcessWire;?>

<!DOCTYPE html>
<html> 

<li class="Pub-profile-info"><?php $content .=page()->Location?></li>
<li class="Pub-profile-info"><?php $content .=page()->Pub_country?></li>
<li class="Pub-profile-info"><?php $content .=page()->Since?></li>
<li class="Pub-profile-info"><?php $content .=page()->Contact?></li>
<li class="Pub-profile-info"><?php $content .=page()->Focus?></li>
<li class="Pub-profile-info"><?php $content .=page()->Members?></li>
<li class="Pub-profile-info"><?php $content .=page()->Location?></li>
<li class="Pub-profile-info"><?php $content .=page()->Decision_making?></li>
<li class="Pub-profile-info"><?php $content .=page()->Financing?></li>
<li class="Pub-profile-info"><?php $content .=page()->History?></li>
<li class="Pub-profile-info"><?php $content .=page()->images?></li>
<li class="Pub-profile-info"><?php $content .=page()->logo?></li>
</html>

and the screen looks like that:

Thanks for helping!

Screenshot from 2019-10-16 11-43-18.png

Share this post


Link to post
Share on other sites

Hi,

No need to concatenate your output which means no need to use <?php $content .= page()->your field; ?>.

Just do this:

<?php  echo page()->your field; ?>

Gideon

  • Like 1

Share this post


Link to post
Share on other sites

Hi @Lmwt, welcome to the forum! Did you go through the hello worlds tutorial step by step? BTW: You know there's someone in Vienna that knows ProcessWire quite well and is always willing to help? 😉 

Share this post


Link to post
Share on other sites
3 hours ago, ottogal said:

The <li> elements have to be children of an <ul> element (alternatively of an <ol> element).

 

... and those have to be children of body, not html :-)

7 hours ago, Lmwt said:

due to delayed output

How do you include this list in your main template? regular include() ? In that case, you don't output anything in that partial, just build a string with PHP that you then echo() in your main template. You should definitely look into some beginner tutorials and docs - templating in PW really isn't hard to grasp. For the above list, you could use PHP's heredoc syntax. btw, for your last two fields you would need slightly more, since these are probably file/image fields (like $page->images->first()->url or whatever you need)

  • Like 1

Share this post


Link to post
Share on other sites

Thanks for all the tips! I will definitely go through these hello world tutos they are super clear.

@ Gideon So If I just go echo page()->your field; then the text is displayed above the nav bar, as shown on the screenshot. @dragan: I shouldn't need to include anything, from what I read on delayed output in the "structure your templates" tutorial. The _main.php template is just appended to the template code and if I add an extra html tag into it, it still gets output above the navbar... maybe there is something to look for in the region(...) method, but I couldn't set it up... (I am a beginner also in php language).

In the meantime I found this way to do it. It's working. The problem is that I can't put the second array between <li> tags because the " ." in  "$content .= $page->get($field->name) . " gets read as a string...so i can't apply any style to it 😕 anyone comes up with a different way to put that?

<?php namespace ProcessWire;?>


<?php  foreach ($page->template->fields as $field) {
    $content .= "<li class='pub-field'>$field->label: <br> </li>";
    $content .= $page->get($field->name) . "<br> <br>"; 
} 

 

Share this post


Link to post
Share on other sites

Just paste the content of your _main.php file and your ##yourtemplatename##.php file and we can show you what to do 😉 

Share this post


Link to post
Share on other sites

Here is my _main.php code:

<?php namespace ProcessWire;

/**
 * _main.php
 * Main markup file (multi-language)

 * MULTI-LANGUAGE NOTE: Please see the README.txt file
 *
 * This file contains all the main markup for the site and outputs the regions 
 * defined in the initialization (_init.php) file. These regions include: 
 * 
 *   $title: The page title/headline 
 *   $content: The markup that appears in the main content/body copy column
 *   $sidebar: The markup that appears in the sidebar column
 * 
 * Of course, you can add as many regions as you like, or choose not to use
 * them at all! This _init.php > [template].php > _main.php scheme is just
 * the methodology we chose to use in this particular site profile, and as you
 * dig deeper, you'll find many others ways to do the same thing. 
 * 
 * This file is automatically appended to all template files as a result of 
 * $config->appendTemplateFile = '_main.php'; in /site/config.php. 
 *
 * In any given template file, if you do not want this main markup file 
 * included, go in your admin to Setup > Templates > [some-template] > and 
 * click on the "Files" tab. Check the box to "Disable automatic append of
 * file _main.php". You would do this if you wanted to echo markup directly 
 * from your template file or if you were using a template file for some other
 * kind of output like an RSS feed or sitemap.xml, for example. 
 *
 * 
 */
?><!DOCTYPE html>
<html lang="<?php echo _x('en', 'de'); ?>">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<title><?php echo $title; ?></title>
	<meta name="description" content="<?php echo $page->summary; ?>" />
	<link rel="preload" href="<?php echo $config->urls->templates?>styles/main.css" as="style">
	<link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>styles/main.css" />

	<?php
	
	// handle output of 'hreflang' link tags for multi-language
	// this is good to do for SEO in helping search engines understand
	// what languages your site is presented in	
	foreach($languages as $language) {
		// if this page is not viewable in the language, skip it
		if(!$page->viewable($language)) continue;
		// get the http URL for this page in the given language
		$url = $page->localHttpUrl($language); 
		// hreflang code for language uses language name from homepage
		$hreflang = $homepage->getLanguageValue($language, 'name'); 
		// output the <link> tag: note that this assumes your language names are the same as required by hreflang. 
		echo "\n\t<link rel='alternate' hreflang='$hreflang' href='$url' />";
	}
	
	?>
	
</head>

<body class="<?php if($sidebar) echo "has-sidebar"; ?>">

	<a href="#main" class="visually-hidden element-focusable bypass-to-main"><?php echo _x('Skip to content', 'bypass'); ?></a>

	<!-- language switcher / navigation -->
	<ul class='languages' role='navigation'><?php
		foreach($languages as $language) {
			if(!$page->viewable($language)) continue; // is page viewable in this language?
			if($language->id == $user->language->id) {
				echo "<li class='current'>";
			} else {
				echo "<li>";
			}
			$url = $page->localUrl($language); 
			$hreflang = $homepage->getLanguageValue($language, 'name'); 
			echo "<a hreflang='$hreflang' href='$url'>$language->title</a></li>";
		}
	?></ul>

	<!-- top navigation -->
	<ul class='topnav' role='navigation'><?php
		// top navigation consists of homepage and its visible children
		foreach($homepage->and($homepage->children) as $item) {
			if($item->id == $page->rootParent->id) {
				echo "<li class='current' aria-current='true'><span class='visually-hidden'>" . _x('Current page:', 'navigation') . " </span>";
			} else {
				echo "<li>";
			}
			echo "<a href='$item->url'>$item->title</a></li>";
		}


		// output an "Edit" link if this page happens to be editable by the current user
		if($page->editable()) echo "<li class='edit'><a href='$page->editUrl'>" . __('Edit') . "</a></li>";
	?></ul>

<div id="site-headline" >
   <?php echo "<a href='{$config->urls->root}'><h1 id='logo'>{$homepage->headline}</h1></a>"; ?>
</div>

	<main id='main'>
		<!-- main content -->
		<div id='content'>
			
			<h1><?php echo $title; ?></h1>
			<?php echo $content; ?>
			
		</div>
		<div id='pubs-ul'></div>

		<!-- sidebar content -->
		<?php if($sidebar): ?>
			
		<aside id='sidebar'>
			
			<?php echo $sidebar; ?>
			
		</aside>
			
		<?php endif; ?>

	</main>


	<!-- search engine -->
	<form class='search' action='<?php echo $pages->get('template=search')->url; ?>' method='get'>
		<label for='search' class='visually-hidden'><?php echo _x('Search:', 'label'); ?></label>
		<input type='text' name='q' id='search' placeholder='<?php echo _x('Search', 'placeholder'); ?>' />
		<button type='submit' name='submit' class='visually-hidden'><?php echo _x('Search', 'button'); ?></button>
	</form>

	<!-- footer -->
	<footer id='footer'>
		<p>
		
		<div class="col">
       
		<?php
		if($user->isLoggedin()) {
			// if user is logged in, show a logout link
			echo "<a href='{$config->urls->admin}login/logout/'>" . sprintf(__('Logout (%s)'), $user->name) . "</a>";
		} else {
			// if user not logged in, show a login link
			echo "<a href='{$config->urls->admin}'>" . __('Admin Login') . "</a>";
		}
		?> 
		<address>Kienitzerstr. usw Neukölln, Impressum</address>
		<a href='http://www.fairyfiles.org/en/contact/'><?php echo __('Email: team@fairyfiles.org'); ?></a> &nbsp; / &nbsp; 
		</div>
		
    <div class="col">
    Thumbnails here?
    </div>
    <div class="col">
    	<div class="fb-like-box" data-href="https://de-de.facebook.com/QueeresVerlegen/" data-width="255" data-height="280" data-colorscheme="dark" data-show-faces="true" data-header="true" data-stream="false" data-show-border="true"></div>
    </div>
			
		</p>
	</footer>

</body>
</html>

and here is my publishers.php template code:

<?php namespace ProcessWire;?>


<?php  foreach ($page->template->fields as $field) {
    $content .= "<li class='pub-field'>$field->label: <br> </li>";
    $content .= $page->get($field->name) . "<br> <br>"; 
} 

Thanks A LOT !

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Pip
      Hi Everyone!
      I'd like to set a different label on my Title (PageTitle) field in one particular template.  Is there a way to do so? 
      Thanks. 
    • By benbyf
      Hello, and welcome to what I though was either my client being silly and changing things, or some evil doer. Turns out its reproducible and therefore something in Proceswire (I checked my templates and modules but couldnt find anything that would be doing this...). So what is it doing? Check out the video for evidence.
      A repeater field is interacting with a page template and another repeater field somehow to swap the fields in the template and repeater over...
      I have a template called team, and a repeater field called team_repeater with label Team. Some how and for some reason, when I change my fields on repeater called main_menu_links my team template gets those fields and when I try and revert the team template fields to the fields it should have, they get given to the repeater main_menu_links. Also this to say HELP!!!!!
      video: https://www.dropbox.com/s/exkdhc6n7x0xpsa/strange-repeater-PW-mega-bug.mov?dl=0
    • By DV-JF
      Hey all, hope you're feeling well these days!
      Short question: Under "Setup > Templates" it's possible to sort the templates by last modified. Is this possible for fields view to?
      Long explanation:
      Currently I am in the process of programming a reusable template and often have to copy several fields & templates from different PW installations into my "master version".
      Therefore it would be good to see already in the field list when the last modification has taken place.
      Stay healthy & don't forget to wash you hands - many greets!
    • By Lmwt
      Hi forum,
      I have this list of fields with labels and I want to skip the displaying of labels for fields left empty (e.g. if field "logo" is not filled with an image, but it could also be text fields). Using count() and "continue" will always output either nothing or all labels.
      Is there a way to use the "skipLabel" method in the loop?
      foreach ($page->template->fields as $field) { if(!count($page->$field->logo)) {continue;} else { $etikette = $field->getLabel(); //get label in the current user language $content .= " <br><li class='pub-field'>$etikette<br> </li>"; //display labels as li $content .= $page->get($field->name) . "<br>"; // display values as li } } Thanks for your help!
    • By louisstephens
      So I was tinkering around with the "select fields" field type and added it to a repeater. My thoughts were I could have a user select a field (textarea, text, etc etc) that I defined and give it a name (another field in the repeater) and create their own form on the page. To be honest, I am now a little lost with rendering the form and mailing the results as potentially the form will be unique and custom every time.  The only way I know to handle the output is by going about it this way:
      $forms = $page->form_select_fields; foreach($forms as $form) { if($form->name === "form_input") { //output input with custom name } elseif($form->name === "form_textarea") { //output input with custom name } } Is there a better way to go about rendering the elements from the repeater? As far as the custom sending goes, I am really at a loss since it would be pretty dynamic. Has anyone used this type of approach, and if so, how did you handle this without going insane?
×
×
  • Create New...