How to Display the Text contents of a text file in yii? - php

I am trying to display the contents of a text file inside a div in my view. I found that in php
<?php
$myfilename = "mytextfile.txt";
if(file_exists($myfilename)){
echo file_get_contents($myfilename);
}
?>
this code can be used . Is there any such methods in Yii to display the contents inside a text file?
Update
This file is stored under components folder. It cannot be included inside the views section.

readfile()
file_get_contents
You can try these two functions. :)

There are renderFile() method in CController class.
Put your text file somewhere at views/files and use that method like
$this->renderFile('/files/filename.txt');

Related

How can we read the content of html file which is stored in views folder in laravel?

I am trying to load the html file stored in views folder in laravel.
I just want to get the path of that html so that I can read the content.
Is there any way to do this?
for a workaround am storing that html file in storage folder and reading the html content. But as I said this is just a workaround for me.I need to store that in views.
If you want to just get the html content of a view, you can simply do:
$html = View::make('your_view');
While, if you want to get the content of a json file (as you've asked in the comments), you should put it in the storage folder and get its content with:
$content = Storage::get('file.json');

Yii render variable not view file

question is: Does Yii have any method(s) to render variable with code in it?
Default:
$this->render('site/index'); where site/index is path to view file.
What I need to do is:
$content = '<div><?php echo "do something here"; ?></div>';
$this->render($content);
Output should be layout + parsed content
I tried to use $this->renderText($content); but this methods returns empty string.
I'm using Smarty extension to render view files, then $this->renderText($content); returns not parsed string: {assign ..}
Any help would be appreciated.
There are some rendering Functions in Yii you could use to get there, but not with a variable. It would be better to use renderFile(). If you want to pass data to this file use
$this->renderFile("renderfile.php",array("var1"=>"xyz","var2"=>"abc");
More render funtions described here

Generating PDFs from template files using wkhtmltopdf

I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>

replacing file contents in php and saving to a new file

So I would have a template file called template.php and inside, it would have:
<html>
some other content
<?php $name ?>
</html>
Essentially, I want to pull the name from the database and insert it into $name and save that entire page as a new file.
So let say in the database, I had a name called Joe, the new file I create would have the following content inside:
<html>
some other content
Joe
</html>
The only issue I am having is finding the right functions to actually replace the variable from the template file, and being able to save it into a new file.
That's not how templates usually work. You should have a template file, substitute variables for their values, and display it to the user. You can cache these generated templates (the ones that are always the same) but you never make a new template file for each user and save it permanently. That makes managing and updating the application way more difficult than you want.
I recommend using a simple template system like RainTPL. That's what I use for most of my projects... it's very simple to use and provides all the basic functionality you would need.
You can also use just PHP for this... there are a few answers on SO that cover this so I won't get into it.
Using PHP as template engine
Using Template on PHP
Look at the answer I gave to this question a while back. There I explain how "variable parsing" usually works.
When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.
Here's an example of how to use output buffering with PHP to create what you're wanting.
The Template template.php
<html>
some other content
<?php $name ?>
</html>
Where your database code and stuff is index.php
<?php
$name = 'John Doe';
if( /* some sort of caching system */ ) {
$parsed_template = file_get_contents('parsed_template.html');
}else {
ob_start();
include 'template.php';
$parsed_template = ob_get_clean();
file_put_contents('parsed_template.html', $parsed_template);
}
echo $parsed_template;
?>

How to switch layout files in Zend Framework?

I'm sure it's a simple one-liner, but I can't seem to find it.
How can I use a different layout file for a particular action?
Update: This worked for me, thanks!
// Within controller
$this->_helper->_layout->setLayout('other-layout') //other-layout.phtml
//Within view script
<?php $this->layout()->setLayout('other-layout'); ?>
From inside a Controller:
$this->_helper->layout->setLayout('/path/to/your/layout_script');
(via these docs)
EDIT: I should mention that the path is relative to whatever your layout directory is (by default, it's application/layouts/scripts/)
You can also use like this
// Within controller
Zend_Layout::getMvcInstance()->setLayout('layout_name');
//Within view script
<?php $this->layout()->setLayout('layout_name'); ?>
Your layout must be in /layouts/scripts/ folder, otherwise you need to specify the path also. No need to write .phtml, just name of the layout

Categories