Smarty Remove Whitespace - php

I'm using Smarty with CodeIgniter.
Here's my code:
<!doctype html>
<html lang="en-gb">
<head>
<title>Frustrating as Hell</title>
<meta charset="utf-8">
</head>
<body>
{if $test == 'hello'}
Hello!
{/if}
</body>
</html>
If I view-source on Chrome, I see this (removed unnecessary parts):
<body>
Hello!
</body>
I want it to be nested correctly like so:
<body>
Hello!
</body>
How would I do this? Please don't suggest {strip}{/strip}, that outputs <body>Hello!</body> which I don't want.
Thanks!

I don't really understand why you would want it to look good in chrome's view-source but if I were you, I'd play around with the source
<!doctype html>
<html lang="en-gb">
<head>
<title>Frustrating as Hell</title>
<meta charset="utf-8">
</head>
<body>{if $test == 'hello'}
Hello!{/if}
</body>
</html>
I don't know if this will work, but either way, you will end up having your working code not neat or the result not neat. If I had choice, I'd chose the resulting code not neat, because in fact nobody will look at it (well I guess) ;-)

Related

Configure apache server to run Python in special tags (like PHP)

Is it possible to configure an Apache webserver to use execute Python in the same way as PHP? Meaning, you could execute python code in a <?python3 ?> tag just like you can execute PHP in <?php ?> tags. I am not asking about WSGI or CGI.
Here's an example of what it would look like in Python3:
magic_number.py:
<?python3
import math
number = math.sqrt(42)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magic number</title>
</head>
<body>
<h1>The magic number is <?python3 print(number)?></h1>
</body>
</html>
The same code in PHP would look like this:
magicNumber.py:
<?php
$number = sqrt(42);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magic number</title>
</head>
<body>
<h1>The magic number is <?php echo $number; ?></h1>
</body>
</html>
If this is possible, how could I configure it to work in Apache?
Any help is appreciated!
You can create behavior you want, but I'm not sure it's a perfect way to solve end task you are trying to do.
The way what would render your php script. It will be your entry point. I think it's Web page itself, which mean that you need to call shell. For ex.
<?php
$output = shell_exec("python embedded.py");
?>
<!DOCTYPE html>
<html lang="en">
<head>
....
Now above will execute file, which is not exist yet. To create it, you would need to call php to create file from string. Below complete snippet.
<?php
// Create file with Python source
$embedded = <<<EOF
import math
number = math.sqrt(42)
EOF;
// Save file somewhere
file_put_contents("embedded.py", $embedded);
// Execute shell on Server side
$output = shell_exec("python embedded.py");
?>
<!DOCTYPE html>
<html lang="en">
<head>
....

PHP: Why does preg_match give different results when it is inside a.html-file or a .php-file (unicode)?

Recently I found preg_match giving different results when the PHP-code is inside a .html-file or a .php-file!
It seems like it has something to do with the RegEx-Modifier "/u". When this flag is set and inside the teststring is an unicode-character(like the "Ö" in the examples),
it will give different results.
Does anyone have an idea why this could be?
Example 1 - .html-file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
$test="asdddddAÖ";
echo preg_match('/^[[:print:]\s]*$/u', $test);
?>
</body>
</html>
Example 1 - Output:
0
Example 2 - .php-file
<?php
$test="asdddddAÖ";
echo "
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>Document</title>
</head>
<body>
".preg_match('/^[[:print:]\s]*$/u', $test)."
</body>
</html>";
?>
Example 2 - Output:
1
If I delete the unicode-character from the teststring the result of both is
1
which I expected.
Also if I delete the /u-Flag from the RegEx the result of both is
0
as expected too.
Both examples ran under the same apache-instance with php 5.6.24. And both files were encoded UTF-8 in the editor. It is also an Plesk 12 environment on Ubuntu 14.04 if that helps.

"//" on top of every html page using laravel 5 blade layout

I'm creating web application using laravel 5. Everypage has "//" on up-left corner. What is causing this?
The app.blade.php looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
#yield("content")
<p>Above content generated by MVC</p>
</div>
</div>
</body>
</html>
Well it certainly isn't anything in the blade template that's doing it.
What is probably happening is that you've got somewhere in your code a line which says echo "//"; or something similar, or a rogue line of code before your <?php block starts -- maybe you were trying to comment out a block of code that includes a <?php block.
That line doesn't have to be in the template; it could be anywhere in the code; if it's run before the template is output, then you will get the kind of effect that you're reporting here.
As for where the line is and what it's doing there, that's something you'll have to work out for yourself. But you can start by searching your codebase for echo or print statements, and for //<?php.

How can I have a doctype file for each page and a title for each page?

My crazy web teacher requires a doctype.php file to be in each of my webpages to avoid repetition, like this:
<?php include 'doctype.php'; ?>
However, I also need a different title for each page.
<head>
<title>Activities at Pacific Trails Resort</title>
</head>
This doesn't pass in http://validator.w3.org/. How do I include the doctype and the title?
In your doctype.php page add the following:
<!DOCTYPE html>
(The trailing empty line is intentional, you will need to ensure it exists in the doctype.php file.)
In each of the other pages, they should look something like:
<?php include 'doctype.php'; ?>
<html>
<head>
<title>Activities at Pacific Trails Resort</title>
</head>
<body>
<!-- content -->
</body>
</html>
Then when you visit each page, the output will be something like:
<!DOCTYPE html>
<html>
<head>
<title>Activities at Pacific Trails Resort</title>
</head>
<body>
<!-- content -->
</body>
</html>
you could always make yourself a little function for the start of your page and pass the title as a variable. You would put the function in your config.php and then all you have to do is require "config.php" and start your page with startPage("your title").
function startPage($title)
{
echo<<<END
<!doctype html>
<html>
<head>
<title>$title</title>
<link rel='stylesheet' href='css/admin.css' type='text/css'>
</head>
END;
}
ETA: I've gotten 2 down-votes for this but no explanation why. It might be breaking php's style guidelines because I'm echoing html? Not sure?

Is it possible to write the contents of b.aspx on a.aspx?

Here is the scenario: I have two asp pages. a.aspx is layout and b.aspx is content. I want to display the contents of b.aspx inside a <div> on a.aspx. I know with PHP you can do it like so:
//a.php
<html>
<head>
<title>test</title>
</head>
<body>
<?PHP
include "b.php";
?>
</body>
</html>
//b.php
<?PHP
echo "Content String";
?>
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
Thanks!
This scenario is handled by masterpages and or composing the page out of (user)controls in ASP.NET. As described at for instance here.
Probably Server.Execute will help.
//a.aspx
<html>
<head>
<title>test</title>
</head>
<body>
<% Server.Execute("b.aspx"); %>
</body>
</html>
//b.aspx
Content String
//result
<html>
<head>
<title>test</title>
</head>
<body>
Content String
</body>
</html>
By the way, I do not recommend this approach. It's just to show it can be done. Master pages and user controls are normally the way to go.
create a B.ascx that does everything you need, and then both B.aspx and A.aspx can include that control.
It sounds like MasterPages will accomplish this for you. Is this not an option for you?
you can go old-skool and use an IFRAME
alternatively, could use a WebRequest in a.aspx.cs to open b.aspx, store the results in a string, and return that string inside a div on a.aspx

Categories