I'm using Twig as a Template Engine.
I want to render my CSS files though Twig, with the advantage of macros.
The response I receive looks fine. But somehow if I include it, the HTML page doesn't recognise it as a css file.
When I include the real css files, the site works just fine.
The comparison of the received source code is exactly the same...
Although, Chrome displays the files differently.
It displays the rendered file on one line.
It is probably being interpreted as a HTML file.
The real css file is being displayed like the source code, on multiple lines.
You include both files with the extension of .css.
How can I fix this?
You say that you get the correct contents, but it's not interpreted as css. This could be because of an incorrect contenty-type header.
Css files should be sent as text/css, in Sf the default is text/html. In the action that renders your css make sure to set the correct header.
Perhaps you should take a look at LESS its more effizient instead of parsing your CSS files. With LESS you have a lot of possibilities to make blocks and reuse functions with variables.
Perhaps its an alternative for your problem.
Edit:
Template Suffix
Run CSS file through Twig when using {% stylesheets %} tag in Twig with Symfony2
Related
I'm using OctoberCMS, based on Laravel and Twig, with Nginx and PHP7.0-FPM.
I make a Page or Partial using the CMS Backend Editor. Can edit the HTML Markup and Code.
The Page will render as localhost/mypage and hides the php source code.
But I'm able to go to localhost/themes/mysite/pages/mypage.htm in the browser and view the Twig markup and PHP comments in plain text.
And on some pages I can view all of the PHP and Laravel code like connection to database names and tables.
Anything that is in function onStart() in the Code editor, even though its wrapped in php tags in the htm file.
mypage.htm output:
However when viewing a .php file, it only shows the output and not the source.
I tried to change the page file extension to php instead of htm but get the error.
Invalid file extension: php. Allowed extensions are: htm.
Have you setup nginx correctly to blacklist those and other unpermitted files? http://octobercms.com/docs/setup/configuration#nginx-configuration
Alternatively, you can take a whitelist approach to security and utilize the october:mirror command: http://octobercms.com/docs/setup/configuration#public-folder
I need to add a meta tag to all files of a website, so I thought about adding that meta tag by PHP code, within header.php, menu.php or footer.php; something like:
<?php addToHeadTag("Meta: favicon='./img/favicon.ico'"); ?>
It has a lot of files and manually changing each file is out of hand.
P.S.: All possible files in which I could include that code are outside head.
First of all, you can place an include file wherever you like in your code. Though that really has nothing to do with the problem at hand.
You should not be using header() for this. header() provides the client browser with meta information on the http response as a whole.
Take a look at this link for usage information on favicons:
http://en.wikipedia.org/wiki/Favicon
In most cases you can just place the favicon.ico in your web root and the browser will pick it up automatically without any markup at all. You can also use a <link> element.
I downloaded a Template + CSS File for a Website that I'm Building, the template worked well until I tried to break it down and put every code in its own file (for easy modification and editing in the future).
So, when I cut the head part which included (Title + Meta Data .. etc ), and put it in its own file, and replaced it (for sure) with an include() function, I lost the CSS styles and returned to the basic & standard style (Black & white with no extra format .. etc)
Where did I Go wrong? Knowing that here is the include function that I've used:
<?php
include 'files/head.php';
?>
With an URL like file:///C:/xampp/htdocs/test6/index.php PHP is NOT executed. You must run it with apache being involved. Currently you are opening your PHP script as a regular txt or html file - it is just passed to browser without processing.
In order to make include function work you must run it with apache. As you are using xamp, I think you should simply open it with URL like http://localhost/test6/index.php In this case, apache will get that request and pass it to PHP. PHP engine will interpret your PHP script and "replace" include files/head.php with a content of head.php.
If everything is Ok, after pressing Ctrl+U (or looking at HTML with Developer Tools or Firebug) you should see a content of head.php instead of <?php include ....
Please note that css files should be linked with relative URL like css/screen.css. Or absolute URL like http://localhost/test6/css/screen.css. like Search for relative and absolute URLs in google for more info.
I've created my own templating/viewing engine to use with Codeigniter. In it I'm able to specify certain css/js files to use with a specific view. I assign the file names in an array, which will then get looped through while echoing the necessary <link href="X"..., <script type="X"..., etc for the respective file type in the header file of the template.
The problem is that I can't seem to use the resources I'm trying to include. The CSS/JS files aren't working even though they're being included and embedded and everything looks right in terms of the syntax in the HTML source code.
My theory is that because I'm using echo to actually print the link/script object into the HTML, that it's actually not really an object that HTML can recognize? Kind of like trying to echo an object in PHP - it doesnt work.
Any advise?
It does not matter if you use plain HTML of php generated code. For the browser it is all the same.
You need to check your source code, and check if the scripts you include are accessible. So copy/paste the src="blabla" from your source code from your browser, and paste it in the address-bar and see what happens.
It is definitely not PHP's fault.
I need one advice from you. I am working on a website, which uses PHP and HTML. As the biggest part of the header and footer code will be same for many pages, I am thinking of using PHP's include to avoid code duplication. But, each of those pages requires different stylesheets and JS files included. What do you think how could I let the other file know what scripts and stylesheet to import?
Our company does this:
The header reads the filename of the page calling it when it's included.
Then, it changes the extension to '.js' and outputs that if it exists. Same for CSS.
So if I have a page "register.php", it will auto-include "register.js" and "register.css" if they exist.
Here's what I do:
<?php include("includes/headContent.php"); ?>
<title>Page title goes here!</title>
<script src="script_only_used_on_this_page"></script>
<?php
require_once("includes/siteHeader.php");
?>
Site Content Goes Here!!
<?php
require_once("includes/siteFooter.php");
?>
Head Content includes any PHP I want included in every page, as well as the opening html and head tag, and any Javascript libraries and css stylesheets I want on every page. Site header closes the /head tag, and opens the body as well as printing out my site header and some other markup that goes on every page. Finally Site Footer closes out my template. Everything in between is my content area!
There are lots of different ways you can do templating, if you wanted to create a simple include and an echoHeader() and an echoFooter() function... just have the echoHeader function accept a parameter which you would pass your javascript and CSS lines to.
you can use MVC coding pattern