Linking images to routes in Laravel - html

I'm working on laravel php framework , I've a simple question ,
While using blade template engine , I normally use html helper functions
to render forms , links or whatever so .
Now when i try to put a link to specific route , i usually to like that :
{{ HTML::link_to_route('author','TheAuthor')) }}
First parameter takes the route , and the second one takes the string that will appear ,
To make it simpler that code will produce :
TheAuthor
Now I want to replace TheAuthor with an image ,
What can i do ?

Or you can use {{route}}
Ex:
<img src="'.$image.'" />

I know this is not the answer you want to hear - but you cannot pass any image via link_to_route.
The problem is the output from the HTML class is escaped automatically. So if you try to pass this:
{{ HTML::link_to_route('author','<img src="'.URL::base().'assets/images/image.jpg" alt="icon" />')) }}
it comes out like this:
<img src="http://laravel3.dev/assets/images/image.jpg" alt="icon" />
which will just be text on the screen - no image. Instead you need to use URI::to_route('author') and generate the link yourself. So make a helper a like this (not tested):
function link_to_route_image($route, $image)
{
$m = '<a href="'.URL::to_route($route).'">'
. '<img>'.$image.'</img>'
. '</a>';
return $m;
}

It may become tedious if you are doing this all over the place, but in isolated spots the best way to handle this would actually be to wrap your entire link_to_* method call in HTML::decode.
{{ HTML::decode(HTML::link_to_route('author','<img src="{URL::base()}assets/images/image.jpg" alt="icon" />')) }}
There's nothing you can do from the inside of the function (such as wrapping the "title" portion in decode) because the cleaning is happening within the function call, but if you decode the entire link it will render the html properly.

Related

HTML image not loading the image src

Hey guys I'm making a TodoApp in react js, and I have a simple layout and I wanted to add a few images to the app. But when I add the images it is not loading that image.
Here is the code
import TodoList from './components/TodoList';
import imagem from './skate.png'
function App() {
return (
<div className="todo-app">
<TodoList />
<img src="imagem"/>
</div>
);
}
export default App;
And here is an image of what´s happening:
When using an bundler (like Parcel or Webpack) to handle images, you need to remember that the imported value is a variable.
You pass variables to props with { and }. Using a pair of " gives you a hardcoded string.
<img src={imagem} />
So,
In React.js (jsx) things don't work quite like HTML. WHat you are doing is importing the image from its location, by using:
import image from './skate.png';
and then, inside of your function.
If you import anything (image, css file, json file), for you to be able to use it you need to call it.
for example:
import JsonData from './data.json'
function App(){
return(
<div>
<JsonData/>
</div>
)
}
To call it.
SO in you case, you just need to replace
to
and it should work.
How about if you do this:
<img src={imagem} />
otherwise, it gets interpreted as a string and you have no http://../imagen on your site.
The problem with your code is that
"imagem"
is read as a String. To read it as an image, place {} around it as so:
{imagem}
"imagem" is string. Need variable {imagem].
when you use qoutes "" in jsx, it consumes it as a string and when you use {}, it consumes it as normal JS, so in your case, if you use
<img src={imagem} />
that would solve your problem

Getting a route not defined error in Laravel Blade

I am a Laravel/web newbie, and am working with a Laravel application inherited from someone else. In the blades, I see two ways in which forms are written.
<form action="{{ route('getdata') }}"
{!! Form::open(array('url'=>'getdata','
I think in the first case, the form starts in HTML format and route alone is defined in laravel format whereas in the second case, even form is defined in Laravel format. Which is preferred and what are the differences? I tried replacing the second format with the first and got a Route [/getdata] not defined error. Laravel Version is 6.
In the first example:
<form action="{{ route('getdata') }}"
You are writing plain html, except for the {{ route('getdata') }} part, that you can read similar to <?php route('getData') ?>. Basically, just the action (the url) of the form is a PHP call to a function that will echo the url in that position, while the rest is plain html (an hardcoded text).
In the second example, {!! Form::open(array('url'=>'getdata',' you are using a Facade to access a class that will generate an html output (similar to the code from the first example ) and you are passing to the method Open() an url, that will be placed inside the generated html in the action field.
The problem that you have using the second method, is that your are not passing your route, but a string. Change it like this:
{!! Form::open(array('url'=>route('getdata'),'
To fix the error of the undefined route, you should just call name()function at the end of your route:
Route::('your_route_url','controller#method')->name('getdata');
You didn't define any route named getdata acctually.
Change your code route to url :
<form action="{{ url('getdata') }}

Decoding HTML tags in Laravel 5

I am using CKeditor for my Laravel 5.1 custom CMS. When I request content of stored pages in the database on my view, they appear with HTML tags like <p>Hello world</p>.
I have used html_entity_decode() with and without the charset specified to no avail.
It is also worth mentioning that I use Blade template engine at my view. Hence, my demo code looks like
$post->content = '<p>Hello world</p>'; // from the database from controller
{{html_entity_decode($post->content)}} //did not decode
I also tried using it in my controller instead and it did not change anything like this
$post->content = html_entity_decode($post->content); //before sending it to the view
I need your help to address this issue.
Instead of doing {{html_entity_decode($post->content)}},
try this:
{!! $post->content !!}
More info here https://laravel-news.com/2014/09/laravel-5-0-blade-changes/
you can do this in the controller
strip_tags($post->content);
or if you use in blade
{{ strip_tags( $post->content ) }}
see more info here strip_tags

Retrieve data from the server without going to a new web page

I use a frame from a HTML page to load some data from the server without having to leave the original web page. I simply reassign the src of the frame and the shown data gets updated.
Now I need to programatically create a div, but the width and height have to be retrieved from the server. May I use a frame to get those values, without leaving the web page, or is there a more simple and efficiente way ?
I would prefer not to use ajax, and keep my code as simple as possible, thanks
One easiest way is to call page property like this:
<script type="text/javascript">
var width = <%=this.Width %>
</script>
and declare this property in cs file like this
public int Width
{
get;
set;
}
Is it what you expected? or if you have some calculations after pages loaded, you can simply achieve it by using jquery ajax and one httphanlder.
Use jQuery. It's really simple.
A simple example:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$('#result').load('otherfile.php');​
</script>
<div id="result"> </div>
Loads otherfile.php inside the div id'ed "result". No page reload.
Thanks for the jquery example, I may use it in the future. For now I found a very simple and efficient way to solve it using a frame (I know, I know...) that really surprised me for its simplicity:
I create a form (a javascript class that manages several divs), then a little frame inside it, and the frame change the form properties and remove itself.
The key was to access the javascript variable defined in the parent from the child:
echo "<script>";
echo "parent.window.oDlg.SetSize( ".$row[ "WIDTH" ].", ".$row[ "HEIGHT" ]." );";
echo "parent.window.oDlg.SetTitle( '".trim( $row[ "TITLE" ] )."' );";
echo "</script>";

GET request in AJAX-retrieved HTML

There is some HTML code which appears on the page as the result of AJAX request/response. This HTML contains GET requests to another php script. Links containing this requests are not working while "real" HTML (added on the page manually) works normally.
When I open the page source with Ctrl+U I see empty div's (but they actually have HTML retrieved via AJAX) and the full-fledged HTML code added manually. Can't figure out how can I make the AJAX-retrieved links working.
This is how I try to form the HTML code which must be added to the page as the result of AJAX response.
<?php
//some selections from the database...
//all subsequent varibles are the members of $row = mysql_fetch_array($query) array;
$html_to_be_inactive =
'<li id="productID_' . $productID . '">
<a href=work.php?action=deleteFromBasket&productID=' . $productID . ' onClick="return false;">
<img src="images/delete.png" id="deleteProductID_' . $productID . '">
</a> ' . $productName . '(' . $totalItems . ' items) - $' . ($totalItems * $productPrice) . '</li>';
echo $html_to_be_inactive;
?>
And that is it, result of 'echo' appears on the page after successful AJAX request as the simple HTML, and it does nothing when I click the link.
I do not perform DOM 'append' methods as I'm not familiar with DOM yet. I can see all the elements mentioned in $thml_to_be_inactive but the link seems to be really inactive. Also this HTML is absent in the "Ctrl+U" output, I can see it only when I select "Inspect element" in the browser.
The HTML which was added manually operates the same items as usual, but I need AJAX-retrieved HTML to be working as well.
I suspect I need to perform some 'append' method via JavaScript or jQuery, but I don't know how to do it in a proper way.
Thanks.
You need to put your href in quotes.
onClick="return false;" will override the default action (go to href) of your link. You need to bind an other event to the link, or just remove `return false'.
Your links use relative paths.
You're probably inserting the HTML into a page in a different directory, breaking the relative paths.
If so, you need to switch to absolute paths.