Display function in header.tpl - Prestashop - function

I would like to say that i am quite new to prestashop.
I have added FrontController.php to override/classes/controller.
The file contains the following code
class FrontControllerCore extends Controller
{
public function thefunct()
{
return 'AA';
}
}
Now in the header.tpl i try to call the function by using {FrontController::thefunct()}.
When I put the public function in classes/controller it works, but when I put it in the override folder it doesn't.
How do I display the function in my header.tpl?
And how does the override folder work then?

(I assume you use ps 1.5)
If you are overriding FrontController you need to initiate your class like so
class FrontController extends FrontControllerCore
and delete cache/class_index.php file, (I mean, delete it everytime you override new controller or class)
Teach yourself how to override in ps. through excellent docs.
EDIT:
class FrontController extends FrontControllerCore {
//here you should create and assign variables which you want to use in templates
public function initContent()
{
parent::initContent();
//in such a way you assign variables into smarty template
$this->context->smarty->assign(
array(
'acme_variable' => $this->acme()
)
);
}
protected function acme()
{
return "Wile E. Coyote and The Road Runner";
}
}
//header.tpl
<div>{$acme_variable}</div>

Related

How can I call a specific function in my controller

I'm trying to create a module.
On my Dashboard I have a table with all my elements in my database, but, I don't know how I can set a function present in my controller in my smarty template.
Example of my function in my AdminController and who extends ModuleAdminController
public function deleteAction($id)
{
//here my logic
}
In smarty how can I set my link to redirect to my function?
Delete
Try to set your method with public static scope and then call it from you tpl. Something like
public static function deleteAction($id)
{
//do semething here
}
and then call it from tpl like
Delete
But be aware that it would work only from related tpl but not everywhere

How to use renderAjax() directly?

I create helper without extends controller, like this
class SomeClass{
public function static SomeMethod(){
//this function for send email
$controller = new \yii\web\Controller();
$controller->renderAjax("template", "array");
}
}
how ever the above code generate Missing argument 1 for yii\base\Controller::__construct(), called in common\component\BeoHelper.php on line 1715 and defined
How I can fix it?
Try this:
\Yii::$app->controller->renderAjax(...);

Actionscript : Variable not found from another class' function

I tried doing
trace(classname.functionname.variablename);
//or
trace(classname.functionname().variablename);
Didn't work.. any idea, to get from the classname.as the variable, that's inside a function?
Btw i tried making the function static, still didn't work
Any idea?
There's no way, as those variables that are defined inside a function only live as long as the function is executed, and disappear once there's a return or end of function body. In order to get whatever value you want from a function, make a class variable outside the function, assign it the value you want within that function, and address it from elsewhere.
class test {
public static var foo:Number;
function bar():void {
// ... some code
foo=baz*2.54;
// ... more code
}
}
class elsewhere {
...
trace(test.foo);
...
}
the variables created inside a function are only available in the scope of that function.
if the variables are class member variables (declared public on a class);
public class x {
public var varName:String="";
}
you will be able to access them as
classInstanceRef.varName
needless to say you will need to instantiate from that class an instance.
Unless your variable is declared static on the class
public static varName:String="";
and in that case you can access it using
className.varName;

Refer to a Spark view component in code

I'm building a mobile AIR app using Flash Builder 4.5. The initial view in my views package is TestHomeView.mxml. I want to refer to it in one of my .as classes elsewhere in the app, and I'm not sure how to do that.
Theoretically I should be able to add an "id" attribute to TestHomeView.mxml, but FB gives me an error: "id is not allowed on the root tag of a component". The root tag is s:view.
The reason I need to do this is that within another class I make various calculations and then need to pass an array of values to a component in my view class. So in SomeOtherActionScriptClass.as I first assemble the array, myArray, and then in that class I want to do this:
myViewComponent.viewArray = myArray;
If I'm going to do that, I also need to import the view class into the .as class, which strikes me as weird. So is there a simple way to do what I want, or do I have to dispatch a custom event which contains the array, and listen for it in the view class?
EDIT - Based on the below MVC suggestion I did the following in model:
[Bindable]
public class Model
{
private static var myModel:Model;//doesn't let me name it 'model' because
//I have a package named 'model'
public var myArray:Array; //its value set later in model code
public function Model()
{
if ( Model.myModel != null ){
throw new Error( "Only one Model instance should be instantiated" );
}
}
// singleton: always returns the one existing static instance to itself
public static function getInstance() : Model {
if ( myModel == null ){
myModel = new Model();
}
return myModel;
}
Then in the view code I have:
[Bindable] //do I actually need this?
private var myModel:Model = Model.getInstance();
var viewArray = new Array();
viewArray = myModel.myArray;
But it is coming back null. It isn't null when I put a breakpoint in the Model class, but when I try to access it from the view class, it's null. The model itself isn't null, but that variable is.
What am I doing wrong?
Thanks.
First, if you are trying to make a singleton in AS3 you should first create a class, within the same class file as Model, that is used to ensure you can only create the class once.
Add this class at the bottom of the Model class file (outside of the Model class):
internal class SingletonEnforcer{}
Then create the Model constructor like this:
public function Model(enforcer:SingletonEnforcer){ Init(); } // if init code is needed
public static function get Instance():Model
{
if (!myModel){
myModel = new Model(new SingletonEnforcer());
}
return myModel;
}
Now you don't have to throw an exception for creating a second instance because it isn't possible.
I'm not sure about your first question of referencing your app's main mxml, but if you were asking how to call the app that is running (like WindowedApplication in AIR) then you would call it like this:
// my WindowedApplication file = MyApp.mxml
MyApp(this.parentApplication)
That will return the app's instance.
Once you've set up the Singleton like I have it above you should be able to access your array like:
Model.Instance.myArray;
I hope this helps!
Follow the MVC pattern.
Create Model class (make it Bindable) with a property viewArray. Bind to this property from your View. And in any other class just change viewArray property of the model. The binding event will be fired and this property will also be changed in your View. To make your Model "visible" from any point, you can make it a Singleton.

making a static/class function outside class

how to make a class function with actionscript, I need to have a few static tool functions that are easily used from other classes like
test = tools.rtrim(xx);
e.g. this does not compile:
package com.my.tools
{
static function rtrim(string:String):String {
return string.replace(/\s+$/,"");
}
}
It needs to be attached to a class type not a package
Try
package com.my.tools
{
public class Tools
{
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}
}
}
You can then use it through Tools.rtrim("yourString");
In case your collection of tools gets big it may be useful to use top-level functions as well.
Specially if you want to reuse a tiny selection of your "tools" in other projects without wasting file size by compiling the unused ones (which happens if you include them all in the same class).
In order to do so, in your package folder you will have to create one file per function. Each file should be named the same way as its related function.
e.g. the content of each file named rtrim.as would look like this :
package com.my.tools {
public function rtrim(str:String) : String {
return string.replace(/\s+$/,"");
}
}
Then you will just have to import the top-level function where you need it :
package my {
import com.my.tools.rtrim;
public class Test
{
rtrim("bla bla");
}
}