Comment in .blade.php template - laravel-5.3

I am new in blade template. How i comment here?
PHP i can comment like this
<?php // echo $games;?>
Laravel Blade
{{ $game }}

In blade syntax, a comment starts with {{-- and ends with --}}
{{-- this is a comment --}}
But to comment out multiple lines, use standard PHP block comments instead, like:
<?php /*
#if ($condition)
{{ HTML::form("foo") }};
#endif
*/ ?>
And never nest Blade and/or PHP code inside of Blade comments.
See also stackoverflow.com/Why Blade comment causes page to crash?

Related

Issue with Timber custom page template for Home page in WordPress

I created page-home-page.twig in my view folder of Timber template. However,
1- If the page home-page set as default wordpress home-page, The template file will not work.
2- If the page home-page set as normal page content, then now the template will be okay.
Can any one advise me where is the issue?
Please view Timber documentation.
I fixed it by doing the following:
1- In my WordPress theme folder /wp-content/themes/my-theme/ I created an Custom Page Template reference to this guide: Custom Page Templates
Using this code:
<?php
/**
* Template Name: Video Template
* Description: A Page Template for Home page Video CMS.
*/
defined('ABSPATH') or die;
use Timber\Timber;
$gantry = Gantry\Framework\Gantry::instance();
$theme = $gantry['theme'];
// We need to render contents of <head> before plugin content gets added.
$context = Timber::get_context();
$context['page_head'] = $theme->render('partials/page_head.html.twig', $context);
$post = Timber::query_post();
$context['post'] = $post;
Timber::render(['page-' . $post->post_name . '.html.twig', 'page-hvideo.twig'], $context);
Where Timber::render(['page-' . $post->post_name . '.html.twig', 'page-hvideo.twig'], $context); pointing us to the new Timber page-hvideo.twig file under theme/view folder.
2- And in my Timber template view folder I added this file page-hvideo.twig with the following code:
{% extends "partials/page.html.twig" %}
{% set twigTemplate = 'single.html.twig' %}
{% set scope = 'single' %}
{% block content %}
<div class="platform-content">
<div class="content-wrapper">
<section class="entry">
{% include ['partials/content-' ~ scope ~ '-home-video.html.twig', 'partials/content-home-video.html.twig'] %}
</section>
</div> <!-- /content-wrapper -->
</div>
{% endblock %}
Where {% include ['partials/content-' ~ scope ~ '-home-video.html.twig', 'partials/content-home-video.html.twig'] %} Will manage the home page custom template from view/partials folder under Timber theme folder as normal.
I hope this will help anyone else.
Thank you so much for everything you provided here.
EDITED:
Please be sure after you add your Home Page custom page template to select it from your post editing screen in WordPress under Page Attributes section.

Converting spaghetti code to Twig

Need to convert script with "spaghetti code" to Twig. Read Twig documentation and got basics working. However, I need advice on how to do everything properly, so no re-conversion is needed later. Let's say current script looks following:
file index.php:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
?>
<!DOCTYPE html>
<html>
<body>
<h1><?php echo $page_message; ?></h1>
<form method="post" action="<?php echo basename($_SERVER["SCRIPT_FILENAME"]); ?>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
<?php include("footer.php");?>
</body>
</html>
footer.php looks:
<?php
$footer_text="blah blah";
?>
<footer><?php echo $footer_text; ?></footer>
As far I understand, my index.php should look like this when converted to Twig:
<?php
$page_message="do it";
function display_dropdown($max)
{
for ($i=0; $i<$max; $i++)
{
echo "<option value='$i'>Option $i</option>";
}
}
$twig_params_array=array("page_message"=>$page_message, "footer_text"=>"blah blah");
require_once("../lib/Twig/Autoloader.php");
Twig_Autoloader::register();
$loader=new Twig_Loader_Filesystem("templates");
$twig=new Twig_Environment($loader);
echo $twig->render("index_template.html", $twig_params_array);
?>
Then I should create index_template.html and footer_template.html (or whatever) with following code:
index_template.html
<!DOCTYPE html>
<html>
<body>
<h1>{{ page_message }}</h1>
<form method="post" action="{{ _self }}>">
<select name="whatever"><?php display_dropdown(10); ?></select>
<input type="submit" value="go">
{{ include('footer_template.html') }}
</body>
</html>
footer_template.html
<footer>{{ footer_text }}</footer>
If I understand right, it's also possible to "include" functions in Twig templates (with some tweaking in templates), so I don't need to rewrite existing PHP functions like display_dropdown(). Because dropdown is not displayed at the moment...
The thing that concerns me is array with variables (passed to Twig render function). Do I miss something, or is it really needed to manually define each variable (like $page_message and $footer_text) before Twig can work?
It seems like a lot of work to do, because in "spaghetti code" if I define variable somewhere, I can access it at any time just by using echo function. Now, it looks I need to view every single variable that exists in PHP code and manually pass it to Twig parameters array. Really?
Since no solution was provided, I found it myself. All you have to do is use such a line of code, so all the variables you have in your PHP file (counting included files) become available in Twig templates, and you don't need to re-write anything when new variables are added to PHP file later (they also automatically become available in Twig):
echo $twig->render("template_file_name.extension", get_defined_vars());
Mindaugas, just changing the technology you use to render views is not going to make the code less tangled.
I think you should go one step further and divide responsibilities, by using an MVC framework, like Symfony, which uses Twig as its default templating language.
Good luck!

access parameter passed by symfony 2.7 on the template

I'm trying to access an array that I passed via the $this-render('url', array) on the the template. I'm following the Symfony book where I got this example, I just can't get it to work.
My Controller
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller{
/**
* #Route("lucky/number/{count}")
*/
public function numberActionTemplate($count){
$numbers = array();
for($i = 0; $i < $count; $i++){
$numbers[] = rand(0, 100);
}
$results = implode(',', $numbers);
$numbers = array(0=>'b', 1=>'a', 2=>'c');
return $this->render('lucky/number.html.php', array('luckyNumberList' => $numbers));
}
My template
<html>
<head>
<h1>Testing</h1>
</head>
<body>
<ul>
<li>
<?php $luckyNumberList[0] ?>
</li>
</ul>
</body>
</html>
At this point im not sure what I'm doing wrong. Basic php experience and started with symfony a few days ago.
Symfony is using Twig templates, while you're trying to use PHP templates.
The normal way to write it is:
<li>
{{ luckyNumberList[0] }}
</li>
You can look at the Twig reference:
http://symfony.com/en/doc/current/book/templating.html
What code did you use? I searched class LuckyController and found this code from the official documentation:
{# app/Resources/views/lucky/number.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Lucky Numbers: {{ luckyNumberList }}</h1>
{% endblock %}
It's important to note that there is no PHP in this Twig template.
If you want to display one value from your PHP Table, you can access to any item from the array with Twig:
…
<h1>Lucky Numbers: {{ luckyNumberList[0] }}</h1>
…
Looks like you forgot to use echo, nothing serious:
<?php echo $luckyNumberList[0] ?>
I suppose that you included php templating engine in configuration:
# app/config/config.yml
framework:
# ...
templating:
engines: ['php', 'twig']

php in .phtml file not parsing

I have problem with my boxbilling script. all the phtml files refuse to parse my php codes. I have already confirmed that all handlers and types have been added and working. Its weird, when i create my own .phtml and put my html and php codes inside it works. And it seems the support for boxbilling is dead. I'm planning to get a wordpress menu from my other site to my current one. using these php codes. Thank you all.
<?php
require( '/home/admin/public_html/wp-load.php' );
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
?>
<?php
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('my_mega_menu') ) : ?>
<?php endif; ?>
Code below is part of the .phtml i have marked an area where the code is suppose to go.
Thank you
<div class="top-buttons">
<a id="login-form-link" class="bb-button bb-button-submit" href="{{ 'login'|link }}">{% trans 'Sign in' %}</a>
<a class="bb-button bb-button-red" href="{{ 'login'|link }}?register=1">{% trans 'Register' %}</a>
</div>
{% endblock %}
{% endif %}
</div>
<div class="clear"></div>
</div>
</div>
<!-- START MENU FROM WORDPRESS -->
<!-- END OF MENU FROM WORDPRESS -->
This code below is was what was displayed on the page.
init(); $wp->parse_request(); $wp->query_posts(); $wp->register_globals(); ?>
UPDATE: I just checked on inspect element my php codes are automatically commented if i use this code only.
<!--?php
require( '/home/admin/public_html/wp-load.php' );
get_header();
?-->
I pasted the code that has already been commented.
This error is caused by the wordpress .htaccess file, which doesn't "support" .phtml files by default, just add :
AddType application/x-httpd-php .php .phtml
to the .htaccess file and the scripts should work.
P.S. If you are not using the .htaccess file for redirects consider deleting it.

Wordpress if else requirements for comments

I am not sure where to start, but I want to add in a symbol or change the css for the comments for registered users. Show a difference between non registered user comments and registered user comments.
How would I go about adding this to my wordpress website?
<div class="commentdetails">
<p class="commentauthor"><?php comment_author_link() ?></p>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your review is pending approval by gsprating staff.</em>
<?php endif; ?>
<p class="commentdate"><?php comment_date('F jS, Y') ?>
IP/Hostname: <small>(coming soon)</small>
<?php edit_comment_link('Edit Comment','',''); ?>
</p>
I want to add make it so that the entire class is a different color if the user is a registered logged in user.
Here's an all php version of Saladin's code using the standard if/else sysntax:
<?php
if ($comment->user_ID) {
echo "<div class='comment_registeredUser'>";
}
else { // The user is not logged in
echo "<div class='commentdetails'>";
}
?>
Putting all the code in php has fixed execution errors for me. Of course, that may have been because I was doing something else wrong.
As comments are displayed in the wp_list_comments() function, you will need to edit the code there. The easiest way to achieve this is to use a simple if/else statement checking whether or not the comment has a user ID associated with it. If it does, that means the comment was made by a registered user.
Of course, as well as this, you will need to create a new CSS class to give the distinction. Here is some example code:
<?php if($comment->user_ID) : ?>
<div class="comment_registeredUser"> <!-- Or whatever you decide to call the CSS class -->
<?php else : ?> <!-- The commenter isn't a registered user -->
<div class="commentdetails">
<?php endif; ?>
// Then include the rest of the code as is
The $comment->user_ID variable will return a true if the comment poster is a registered user and was logged in when they posted the comment. The code above will assign your custom CSS class to the div if it does indeed return true. If not, it will simply apply the standard class and styling.
There is also a really good tutorial for developing themes over at the Wordpress Codex. Definitely worth having a read through if you are unsure on what you need to do to create/edit your WordPress theme.
Edit: Cleaned up the answer and better explained the correct logic.

Resources