picture url from controller to jquery [Symfony 2] - php

I want to have some picture displayed when I click on some links but I don't know how to send them from the controller because everytime I have as src= {{ asset('bundles/fds/...') }}.How can I solve it?
public function getPictureAction() {
$html = '<div id="comment_list_div">';
$html .= '<div id="comment_list_div_img">';
$html .= '<div id="comment_list_div_im">';
$html .= '<img src="{{ asset('bundles/fds/images/Picture.png') }}"/>';
$html .= '</div>';
$html .= '</div>';
$html .= '</div>';
$return = array( "html" => $html);
$return = json_encode($return);
return new Response($return, 200, array('Content-Type'=>'application/json'));
}

The correct way to do this would be to move your html code into a template and render it in your action:
In your controller:
use Symfony\Component\HttpFoundation\JsonResponse;
// ...
public function getPictureAction() {
// load your picture from the db
$content = $this->renderView(
'AcmeHelloBundle:Json:JsonResponse.html.twig',
// pass the picture to your template:
array('imagePath' => $image->getPath())
);
return new JsonResponse(
$content,
200,
array('Content-Type'=>'application/json')
);
}
And your template:
<div id="comment_list_div">
<div id="comment_list_div_img">
<div id="comment_list_div_im">
{# use the vairable name you passed to this template to acces your image #}
<img src="{{ asset(imagePath) }}"/>
</div>
</div>
</div>
Also make sure your assets are in place:
php app/console assets:install
php app/console assetic:dump --env=dev

Related

How to show previous_post_link () / next_post_link () in shortcode - Wordpress

I have this shortcode (I wrote it in functions.php) that shows the read previous and read next links in a single post:
<?php
function prev_next_buttons_post() {
$html = '';
$html .= '<div class="container-single-post-buttons">';
$html .= '<div class="prev-next-buttons">' . previous_post_link('%link', '< read previous') . '</div>';
$html .= 'back to blog';
$html .= '<div class="prev-next-buttons">' . next_post_link('%link', 'read next >') . '</div>';
$html .= '</div>';
return $html;
}
add_shortcode('prev_next_buttons', 'prev_next_buttons_post');
The problem I have is that when I add the shortcode:
<section>
<div class="container btd-container-sm">
<?php echo do_shortcode('[prev_next_buttons]') ?>
</div>
</section>
when inspecting it shows the different structure and the links are outside their containers losing their styles:
I need them to be in the same structure as defined in my shortcode.
How could I solve this? Please help.
This is because previous_post_link and next_post_link do not return a value, but write to the output buffer directly.
You need to use their counterparts that return the value, so that you can then concatenate those into your string - get_previous_post_link and get_next_post_link.
(If you check the source code for the former two functions, you’ll see that they are just wrapper functions that call the latter two, and echo their return values - https://developer.wordpress.org/reference/functions/previous_post_link/#source)

return acf image src in twig

i cant get the acf images to return.
in my single-work.twig (view file) i have
<img src="{‌{ Image(post.meta(‘laptop_image')).src }}" />
and in the single-work.php (controller file) template i have
$context[‘laptop_image'] = get_field(‘laptop_image’);
i also tried
in single-work.php (controller file)
$post->laptop_image = new Timber\Image($post->laptop_image);
in single-work.twig (view file)
<img src="{‌{ post.laptop_image.src | resize(500, 300) }}" />
most of the time i get “unknown” as the result.. I used to get the image id returned but i cant remember how i got that… Any help would be great.
full single-work.php
$context = Timber::context();
$timber_post = Timber::query_post();
$context['post'] = $timber_post;
$context['text_with_image_title'] = get_field('headline');
$context['text_with_image_text'] = get_field('description');
$post->laptop_image = new Timber\Image($post->laptop_image);
$context['ipad_image'] = get_field('ipad_image');
$context['iphone_image'] = get_field('iphone_image');
$context['text_with_image_btn_link'] = get_field('work_url');
$context['text_with_image_btn_label'] = 'Go to ' . $timber_post->post_title . ' Website';
$context['text_with_image_btn_target'] = '_blank';
if ( post_password_required( $timber_post->ID ) ) {
Timber::render( 'single-password.twig', $context );
} else {
Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', '/pages/single-work.twig' ), $context );
}
var_dump($post->laptop_image); //returns image id
full code of work.twig
<div class="col-6 text-with-image__column text-with-image__column--image">
<div class="text-with-image__wrapper">
<img src="{{ post.laptop_image.src | resize(500, 300) }}" />
{{dump(post.laptop_image)}} //returns nothing
</div>
</div>
i realized i was using a macro and therefore
<img src="{‌{ Image(post.laptop_image).src }}" /> works fine, though it needs to be applied as the macro value of the new instance. Not just apart of the template.

Fatal error uncaught error class not found

I'm currently getting this error while migrating to a online provider
Fatal error: Uncaught Error: Class 'app\Http\model\MoviePresenter' not found in /home/index.php:4 Stack trace: #0 {main} thrown in /home/index.php on line 4
This code works locally fine it's just decided to stop working on the host.
below is some of the code
<?php ini_set('display_errors', 1);
$moviePresenter = new \app\Http\model\MoviePresenter; $movieGenreList = $moviePresenter->getMovieGenreList();
function displayMovieList($movieList, $moviePresenter)
{
$html = '<div class="movie-list row">';
$currentURL = \Request::root();
foreach ($movieList as $movie) {
$genreList = $movie->getGenres();
$movieGenreList = $moviePresenter->getMovieGenreList();
foreach ($genreList as $genre){
foreach ($movieGenreList['genres'] as $movieGenre){
if($genre->getID() == $movieGenre['id']){
$genre->setName($movieGenre['name']);
}
}
}
$movieID = $movie->getID();
$image = $movie->getPosterImage();
$poster = '<img class="img-responsive" src="//image.tmdb.org/t/p/w154/'. $image .'" width="195" height="360">';
$html .= '<div class="col-xl-3 col-lg-4 col-md-6 col-sm-12 d-flex align-items-center flex-column justify-content-center h-100"><a
href="' . $currentURL . '/movie/' . $movieID . '">';
$html .= $poster;
$html .= '</a><div class="moviedetails row">';
foreach($genreList as $genre){
$html .= '<a href="'. $currentURL . '/discovery/genre/'. $genre->getID() . '" class="genres">';
$html .= $genre->getName() . '</a>';
}
$html .= '</div></div>';
$html .= '</div>';
return $html;
}
?>
It looks like it's looking within the view (where the snipit is from) for the class instead of the file path.
Can anyone tell me why it's searching the local class instead of the view?
It may be because of case sensitivity issues. I bet your new host is Linux, so your namespace should be:
\App\Http\model\MoviePresenter
or \App\Http\Model\MoviePresenter
instead of app\Http\model\MoviePresenter
The laravel App namespace use Uppercase A.
I don't know which Laravel version you are using. You should give more details regarding your question.
Try importing the MoviePresenter model at the beginning in your file like this
use App\MoviePresenter;
or according to your model file directory like this
use App\Http\model\MoviePresenter;
The error Class 'app\Http\model\MoviePresenter' not found, when you use new class MoviePresenter, but you haven't imported the Class MoviePresenter.
So you should import the class MoviePresenter in the file index.php, try using require() at the beginning in your file like this:
require('app\Http\model\MoviePresenter');
//PUT YOUR OTHER CODE BELOW THIS

What's a cleaner way to output HTML from a Wordpress plugin?

My Wordpress plugin creates a few shortcodes that return blocks of HTML.
When I register the shortcodes, I do so like this:
add_shortcode('bb-loans-form', function() {
return Shortcodes::loanApplicationForm();
});
And here is the static method from the Shortcodes class:
public static function loadApplicationForm()
{
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form');
$html .= '<div class="bb-form-field">';
$html .= '<h2>Loan Application Number</h2>';
$html .= $form->text('loan_app_number')->id('loan-app-number');
$html .= $form->submit('Continue Loan');
$html .= '</div>';
$html .= $form->close();
return $html;
}
This is very cumbersome, and messy. I don't like outputting the HTML like this. I've also used Heredoc, but I had to use string substitution to include important values when the form is rendered.
Is there a better way to store my HTML files? I don't want these files publicly accessible. They would have to live in my plugin directory.
It's not a huge plugin, so I'm not overly concerned, but I'd like to know for future reference if there's a cleaner way to include the needed HTML.
You could just use a single string with concatenations...
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form') .
'<div class="bb-form-field">
<h2>Loan Application Number</h2>' .
$form->text('loan_app_number')->id('loan-app-number') .
$form->submit('Continue Loan') .
'</div>' .
$form->close();
return $html;
It at least keeps the HTML aligned.
I also don't really see an issue with Heredoc, as long as you assign variables and substitute them in:
$form = new \AdamWathan\Form\FormBuilder;
$form_start = $form->open()->action('/apply')->class('bb-loan-form');
$loan_app = $form->text('loan_app_number')->id('loan-app-number');
$submit = $form->submit('Continue Loan');
$form_end = $form->close();
$html = <<<HTML
{$form_start}
<div class="bb-form-field">
<h2>Loan Application Number</h2>
{$loan_app}
{$submit}
</div>
{$form_end}
HTML;
return $html;

Laravel use HTML::image for background-image

how to use HTML::image() for inline styled with background-image such as this html tags:
this line is into my main.blade.php file and that is main skeletion of my template.
<div style="height: 45px;background-image:url('../public/images/header_pattern2.png');border-radius: 5px;position:relative">
in main.blade.php my page view correct template. but after redirect such as login.blade.php images do not show. but after switch to index i do not have a problem.
in public directory i have images,js,css folders and my public is this:
'public' => __DIR__.'/..',
i can remove public directory. how to convert background-image:url('../public/images/header_pattern2.png'); to blade HTML::image() tag?
You may try this:
<div style="height: 45px;background-image:url('{{ asset('images/header_pattern2.png') }}');border-radius: 5px;position:relative">
Update:
Actually, HTML::image() generates an <img /> tag and you are using a div, if you want to use this using HTML::div() then you may create a custom macro.
Update: I've created this custom macro:
/**
* Param $attr : An array of styles
* Param $html : HTML content for div
* Returns HTML DIV
*/
HTML::macro('divWithBG', function($attr = array(), $html = ''){
$style = 'style = ';
foreach ($attr as $key => $value) {
if($key == 'background-image') $style .= $key.':url(\'' . $value .'\');';
else $style .= $key . ':' . $value .';';
}
return "<div $style >$html</div>";
});
You may use it in blade view as:
{{ HTML::divWithBG(array('background-image' => asset('images/8_big.jpg'), 'height' => '45px', 'border-radius'=>'5px', 'position'=>'relative' )) }}
Output:
<div style="background-image:url('http://blog.dev/images/8_big.jpg');height:45px;border-radius:5px;position:relative;"></div>
Rendered Output:
Use the following function to solve it. This is assuming that the image exist.
public function inline($path)
{
$filetype = File::type( $path );
$response = Response( File::get($path), 200 );
$response->header('Content-Type', $filetype);
return $response;
}
I tried this and it worked
background: url('{{asset('uploads/avatars/'.$employee->avatar)}}')

Categories