STRINGS - Combine one or more variables and text strings inside view.php

This is a community-contributed tutorial. This tutorial is over a year old and may not apply to your version of Concrete CMS.
Mar 22, 2016
By siton for Developers

This issue is for beginners - Basic PHP Strings.

1.View.php - Mix text string & variables

A lot of times when you want to create a custom block template you need to change the core block markup and change/add css declerations inside your duplicate view.php.

Mabye you find a JS slider and you need to change the core image slider block output from:

   <div class="ccm-image-slider">
    ...rest of the code

To:

   <div class="my-slider-container my-image-slider blabla">
    ...rest of the code

If you are new to PHP i believe you get a lot of wierd small errors in this procces, so i hope this tutorial will give you ideas where to look in your code.


2.The problem - "unexpected (T_VARIABLE)"

Single and double quotes

If you dont want to insert $vars values inside your string its ok to use either echo 'hello world'; or echo "hello world";. But if you inserting variable values inside Single quotes the $var will not parse (php will print $var as a regular text). See example 1-5.

//Example 1-5. single-quotes & double-quotes
$example = "My Example";
echo 'single-quotes-$example'; // Output: single-quotes-$example (var not parse)
echo "Double-quotes-$example"; // Output:Double-quotes-My Example (var is parse)

Echo function, vars and Double quotes

The echo function uses quotes ("") to define the start and end of a string. But also in html markup we uses quotes ("") to define the start and end of css declaration class="myClass" as shown in Example 2-5.

//Example 2-5. Syntax error beacuse of the quotes around $myClass
<?php
$myClass="myClass";
echo "<h1 class="$myClass">Hello world</h1>"; //Wont work! 
?>

Concrete CMS throw error:

syntax error, unexpected '$myClass' (T_VARIABLE), expecting ',' or ';'


3. Two Ways to solve this issue

3.1. Wrong approach - "Echo spagethie"

<?php
//Example 3-5. Correct example with a lot of echo statements
$myClass = "myClass";
echo "<h1 class="; echo $myClass; echo">Hello world</h1>"; 
?>

In 3-5 Example - We solve the problem by adding some echo statements. Its works fine, But the code is now "dirty", less readable, and easy to get confused in the procces.


3.2. Eascape quotes

Quotes are speacial character. Escape quotes within the string with a slash. How it works? You escape a character by typing a "slash" before it `\"$myClass\".

<?php
//Example 4-5. Correct example with escaping of special characters
$myClass = "myClass";
echo "<h1 class=\"$myClass\">Hello world</h1>"; 
?>

3.3. Concatenation operator

Combine one or more variables and text strings. Concatenation Save you a lot of echo statements for clean and readable code.

  • Step 1: Use single quotes (') for quotes inside your string '<h1 class="'

  • step 2: Add Concatenation '. $myClass .'

  • Step 3: Use single quotes (') for quotes inside your string '">Hello world</h1>'; '

<?php
//Example 5-5. Concatenation strings and variables together
$myClass = "myClass";
echo '<h1 class="' . $myClass . '">Hello world</h1>'; 
?>

4. Read about this issues in PHP official docs:

Recent Tutorials
Setting addon/theme version compatibility in the marketplace
Jan 9, 2024

For developers worn out with setting the latest addon or theme version manually across too many core versions, here is a JavaScript bookmarklet to do it for you.

How to get the locale of a page
Jan 8, 2024
By mandako.

Now, why don't we just have a getLocale() method on Page objects beats me, but here's how you work around it

Using a Redis Server
Jun 16, 2023
By mlocati.

How to configure Concrete to use one or more Redis servers to persist the cache.

Using the Concrete Migration Tool Addon
Apr 27, 2023

How to use the Concrete CMS Migration Tool

How To Add Page Last Updated To Your Concrete CMS Pages
Mar 7, 2023

Concrete CMS has a page attribute you can add to a global area called "Page Date Modified." Here's how to add it

How To Exclude Subpages from Navigation
Dec 24, 2022

How to exclude subpages from navigation - useful for a news or blog link in your main navigation where you don't want all the subpages to appear in a drop down menu.

Improvements?

Let us know by posting here.