Support the ongoing development of Laravel.io →
Input Views Blade
Last updated 2 years ago.
0

what do you mean by remote? is it a view file ?

@include will render a view template file. this does not include php files

you need to use php method include() or include_once()

you can assign the result of that file to a variable like this

 $dataPoints = include('\path\to\file');
Last updated 8 years ago.
0

Thanks for the reply! :)

By remote i do mean a view file, the file is located in

resources/views/partials/dataPoints.blade.php

Ill give your method a whirl!

Thankyou!

0

ok, but why do you want to do that? view templates are supposed to render a view, they should't have executable code. The correct approach would be to pass this array from the controller on to the view.

Although you may define a static method on your model class or a utility class to get this array from the view.

0

So ive tried every possible variation of this :

<?php
	$dataPoints = include('partials/dataPoints.php');
?> 

To which i always get the same result, file not found....where in relation to the blade file is the directory?

For example i've tried

../
/
./

None of which worked!

0

astroanu said:

ok, but why do you want to do that? view templates are supposed to render a view, they should't have executable code. The correct approach would be to pass this array from the controller on to the view.

Although you may define a static method on your model class or a utility class to get this array from the view.

The datapoints file is just an array that im trying to include into some java, they're graph vectors, and dont want to clog up my template file including potentially 1000's of datapoints! :)

0

is the array being generated by a java server ? or did you mean "java script", array to be used in a graph back on the front end ?

0

The second one :) Sorry!

0

Create a new class file on the app directory. let's call it Data

namespace App;

class Data{
	
    public static function getPoints()
    {
	    return [
		    // array data
	    ];
    }
}

on your view file you can call it to get the array easily

{{ \App\Data::getPoints() }} 
0

Ok so this is what i done :

Created a new controller in app/controllers called "dataPoints.php"

In there i added :

<?php
namespace App;

class dataPoints{

    public static function getPoints()
    {
        return [
            // array data
            array("y" => 6, "label" => "Apple"),
            array("y" => 4, "label" => "Mango"),
            array("y" => 5, "label" => "Orange"),
            array("y" => 7, "label" => "Banana"),
            array("y" => 4, "label" => "Pineapple"),
            array("y" => 6, "label" => "Pears"),
            array("y" => 7, "label" => "Grapes"),
            array("y" => 5, "label" => "Lychee"),
            array("y" => 4, "label" => "Jackfruit")
        ];
    }
}

Then in my template file i added :

{{ \App\dataPoints::getPoints() }} 

Returned this error :

Class 'App\dataPoints' not found

Thanks a lot for your help really appreciate it! :)

0

move your dataPoints.php to the app folder. project-folder\app\dataPoints.php

0

Ok so i got a new error now!

htmlentities() expects parameter 1 to be string, array given (View: C:\wamp\www\owtest\resources\views\dashboard\sr-graph.blade.php)

LOL What is?!

0

of course, change {{ \App\dataPoints::getPoints() }} to {{ dd(\App\dataPoints::getPoints()) }} this will print your array on the view

0

Excellent thanks for that! :)

So i now have something new....LOL Im sorry, again thanks for your help!

The data i need to post is this :

array("y" => 6, "label" => "Apple"),
            array("y" => 4, "label" => "Mango"),
            array("y" => 5, "label" => "Orange"),
            array("y" => 7, "label" => "Banana"),
            array("y" => 4, "label" => "Pineapple"),
            array("y" => 6, "label" => "Pears"),
            array("y" => 7, "label" => "Grapes"),
            array("y" => 5, "label" => "Lychee"),
            array("y" => 4, "label" => "Jackfruit")

In that format, whats coming through is this :

array:9 [▼
  0 => array:2 [▼
    "y" => 6
    "label" => "Apple"
  ]
  1 => array:2 [▼
    "y" => 4
    "label" => "Mango"
  ]
  2 => array:2 [▼
    "y" => 5
    "label" => "Orange"
  ]
  3 => array:2 [▼
    "y" => 7
    "label" => "Banana"
  ]
  4 => array:2 [▼
    "y" => 4
    "label" => "Pineapple"
  ]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]
  8 => array:2 [▶]
]

It just prints that on my template file/page

The javascript im using is this :

<script type="text/javascript">
            $(function () {
                var chart = new CanvasJS.Chart("chartContainer", {
                    theme: "theme2",
                    animationEnabled: true,
                    title: {
                        text: "Basic Column Chart using CanvasJS"
                    },
                    data: [
                    {
                        type: "column",                
                        dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
                    }
                    ]
                });
                chart.render();
            });
</script>

I was hoping that the data would be posted as $dataPoints...

0

yea, so on your javascript all you gotta do is replace :

dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>

with (if you're using blade)

dataPoints: {{ json_encode( \App\dataPoints::getPoints() , JSON_NUMERIC_CHECK) }}

or replace with this if you're using just php

dataPoints: <?php echo json_encode( \App\dataPoints::getPoints() , JSON_NUMERIC_CHECK); ?>
0

Perfect! :)

Thankyou, just one last thing...when viewing the HTML (as the graph didnt work) i noted the data as :

                        dataPoints: [{&quot;y&quot;:6,&quot;label&quot;:&quot;Apple&quot;},{&quot;y&quot;:4,&quot;label&quot;:&quot;Mango&quot;},{&quot;y&quot;:5,&quot;label&quot;:&quot;Orange&quot;},{&quot;y&quot;:7,&quot;label&quot;:&quot;Banana&quot;},{&quot;y&quot;:4,&quot;label&quot;:&quot;Pineapple&quot;},{&quot;y&quot;:6,&quot;label&quot;:&quot;Pears&quot;},{&quot;y&quot;:7,&quot;label&quot;:&quot;Grapes&quot;},{&quot;y&quot;:5,&quot;label&quot;:&quot;Lychee&quot;},{&quot;y&quot;:4,&quot;label&quot;:&quot;Jackfruit&quot;}]

Which obviously isnt how i want it to come through as, any suggestions? :)

Thanks a million!

0

i think laravel is trying to escape the json string to html

Replace {{ }} with {!! !!} in the view

0

Perfect thats it! :)

Thankyou so much!

0

A quick question a little off topic i guess...

I have a list of numbers (they're 4 digits) eg. 2245

The format they need to be entered is :

array("y" => 5),

Is it possible to use this list and prepend the suffix thats required? The numbers i have in plaintext, literally just a list, but i need the data as

array("y" => 5),

And i dont fancy adding that format to each number as there are potentially thousands!

Any tips/suggestions would be great!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

RabbitSC2 rabbitsc2 Joined 15 May 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.