Sending a complete lang file to a view [CodeIgniter] - codeigniter

Is there any way to send a complete lang file to a view, so that I can access the $lang-variable inside the view.
At the moment the only thing I can find is sending seperate lines to the view (using $this->lang->line('lang_key')) which sounds very antagonizing if I have a few dozen lines I want to print in the view.
My question is if there's a way to select a specific language file and make the whole $lang array in that language file accessible from a view.

Try this in you view in order to see all your lang array.
print_r($this->lang);

You should put :
$this->load->lang('your lang file name');
in the constructor of the controller from which you load your view.
and use it in the view as :
<?php lang->line('key');?>

Load the translation file in the constructor:
$this->lang->load('trans_file', 'spanish');
An then on the view, you can echo each line by:
<?php echo $this->lang->line('prod_not_found'); ?>

Related

Get the file name BLADE of LARAVEL

is it possible to know which BLADE page you are on? I would like to include a different menu where my VIEW is the MAIN, how can I do this in the template? I check if it is my MAIN and add my different menu there if in case. example: #if(config('page.layout') == 'top-nav');
Is that possible? but with another code, of course
To me it seems like you are thinking with the wrong way. You should know what(which controller, which method) has loaded that view instead of what is this view. And you can pass control variables from there if you want.
In you question, the example you have shown:
#if(config('adminlte.layout') == 'top-nav')
So top-nav is coming form config adminlte.layout which has no relation with blade template or anything else. You can definitely do that if you set your config file based on your intention.
I wanted to pick up which file or URL my BLADE was, I got it this way:
#if(Route::current()->getName() == 'name') ....
##codeHTML
#endif

Laravel - Get HTML of current page or of a view according to a path

I have a view mypage.blade.php and a route.
The url is like : https://example.com/mypage/param1/param2. The route use param1 and param2 and generate the page.
Question 1
In that page, I try to get its HTML code. Is there a way to do it?. I tried render() but I don't get what I want.
Question 2
In the view, can I get the HTML code of an other view by specifying a path ?
You had the right idea. Not sure why it wouldn't work for you.
In the controller, set the view into a variable:
$view = view('myBaseView', compact('people', 'places', 'things'));
Now, if you dump the rendered view variable, you have the page's HTML:
dd($view->render());
To get the html of another view by specifying the path and using the internal controller, you would need to set up some kind of a wrapper or catch so that the view variable is not returned as a view, but rendered out to html as above. Your method would need to trap whatever the original controller was sending before it pushed out the view.
Of course, old school php can get the other page's rendered html too possibly if your server is set to allow this:
$html = file_get_contents('http://mypage.com/');
Something else you might find handy is the Laravel sections method. If you just want to render part of the page you can do so by calling whatever section you want from a partial view:
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
dd($sections['modalContent']); // this will only dump whats in the content section
I don't know what you want to do with this html, but if you wish to display it on a page, once you send it (you'd possibly want to return the view along with a compact of the variable $view... as a normal variable if so), remember to use this format:
{!! $view !!}
HTH

Yii2 Render view from extension

I have Yii2 application which has a regular controller with regular action and its view trying to render a view that's part of an extension. My view is in the 'views/controllerName' folder and I'm trying to reach a view which is in 'vendor/providerName/extensionName/views/extensionController'. What is the right way to do that?
I'm tried the regular render() method with different strings like: extensionController/extensionView, /extensionController/extensionView, //extensionController/extensionView but I keep getting an error message that the file is not found in the main view folder which is not where I want the framework to look at first place.
e.g.
echo $this->render('#vendor/firephp/test');
so in your case
echo $this->render('#vendor/providerName/extensionName/views/extensionController');

Magento Translation via Ajax

I have a little problem with translation mechanism in Magento.
If I call translate mechanism from phtml template like this
<li class="landline"><span class="value"><?php echo Mage::helper('ssg_rates')->__("Landline") ?></span></li>
I get translated value from csv file for example "Vaste Lijn" and this is expected output, however when I do the same in block and get data back via AJAX, like this:
in controller I create block and call method to output html
the method outputs something like this:
echo '<li class="landline"><span class="value">'.Mage::helper("ssg_rates")->__("Landline").'</span></li>';
data returned to phtml via Ajax:
{{{Vaste lijn}}{{Vaste lijn}}{{Landline}}{{Ssg_Rates}}}
Why is that? Why it isn't only translated value like in the phtml template?
Cheers
Paul
Problem was caused by translation inline for frontend turned on in admin. Disabling it solved the problem.

codeigniter - loading a library from a view?

I have some data that I have to display as a table.
I think I should pass the data from the controller as $data['events'] = array(.....
and then load the view to display them.
<?php
$this->load->library('table');
echo $this->table->generate($events);
?>
this doesn't work though - it gives a Fatal error: Call to a member function generate() on a non-object
If I paste the same code in the controller, obviously using ->generate($data['events'] the table gets displayed correctly.
Should I get that views can't load libraries, or I am doing something wrong?
Or maybe should I capture the output of the library in the controller and send that to the view?
If you need to call a library (and its functions) within a view, you can do this:
$CI =& get_instance();
$CI->load->library('library_name');
$CI->library_name->yourFunction();
You should run below code in the controller:
<?php
$this->load->library('table');
echo $this->table->generate($events);
?>
and store the data in variable and then send to the view.
To answer what you are doing wrong, you should know that the CodeIgniter class isn't declared in the view, and that this is the case for a reason - to abstract your PHP code from your HTML. Views should contain minimal PHP code (basic loops, conditions).
With this in mind, you should include your library as normal in the controller as follows;
controller
$this->load->library('table');
$data['events_table'] = $this->table->generate($events);
$this->load->view(....);
In the view, you simple echo the data. Although CodeIgniter allows short-hand tags, you should use standard PHP tags to keep to a convention that will work anywhere you keep your code.
view
<?php echo $events_table; ?>
You can auto-load the library in config/autoload.php
$autoload['libraries'] = array('database','session','table');
Then you can simply call the functions in your view .
echo $this->table->generate($events);
This was useful for me when i was creating a dynamic menu header . i auto-loaded the library which has the function for dynamic menu creation and then i simply called that function from the view , by the way it is bad practice .
That is one wrong approach to MVC. You don't need to load the library in the view, because all views are loaded from one CONTROLLER, so every external Helper or Library should be loaded from the controller and the used or send to the views
Regards,
Pedro
Note the shoulds: CodeIgniter is flexible in that it lets you do things the wrong way. It just makes it a little more difficult. You can do almost everything in the view, even when you shouldn't; but loading helpers, models, and views has to be done in the controller.
In controller or model use
$this->load->library('mylib');
$mylib=$this->mylib;
$data['mylib']=$mylib;
$this->load->view('myview',$data,false);
and than in view you can use library directly:
$mylib->myfunction();
It's not MVC like solution but works if you need.
Simply load library on controller, then use it view.
Controller:
$this->load->library('table');
$data = array(
array(1,2,3,4,5),
array(1,2,3,4,5),
array(1,2,3,4,5),
);
$this->load->view('the_view', compact('data'));
View:
echo $this->table->generate($data);

Resources