How to use PHP with Composition API VueJS 3 - php

Coming from a PHP/jQuery background, where you can have PHP and jQuery code in the same file, for example some file called index.php:
<?php
echo "hey";
?>
<script>
$("#someId").html("abc")
</script>
How can you do something similar with the Composition API in VueJS v3 where you can access access your PHP variables/sessions etc. inside of a .vue file or have your SFC files to include PHP code as well?
I know you can do this by including the vue framework with script tags inside of your index.php file but as far as I'm aware you can't use the Composition API with this method.
I'm probably missing something basic but I haven't been able to find the answer.

Related

PHP require vs grails include

I wanted to know the command which is exactly equal to php require in my GSP (Grails server pages) or in Groovy.
I know I can use <g:include/> but wanted to know is there any command which will fulfill php require in groovy/grails?
An exact equivalent really depends on the context of what require() is being used for in the PHP script.
PHP and Servlet environments operate differently. Using require() in PHP simply locates another PHP script and executes it. That imperative-style operation doesn't apply as well to the more object-oriented Java/Grails/Servlets.
There are a couple of possible equivalents, depending on what you're trying to accomplish:
<g:include/>
Includes the response of another controller/action or view in the
current response
e.g.
<g:include controller="foo" action="bar"/>
What this will do is invoke a different controller/action and insert the response into the current page. This would be similar to PHP if your require() was rendering some markup.
View templates:
If you're just trying to include common pieces of markup in several pages, these might be what you're looking for. You can create template views and use <g:render/> to include them in your GSPs. I suspect this is what you're after, but see my "Update" below for some advice about this.
#page import
e.g.
<% #page import="com.example.mypackage.MyClass" %>
This will make MyClass available to the GSP, which would be similar to require() if the require was specifying some library classes or functions to be used in other PHP scripts. However, using this pretty much screams code smell, since almost anything you'd be using this for would be more appropriate in a controller action or a service.
Update:
Seeing your other question, I'll venture you're simply trying to include a common piece of GSP/HTML in several different views, which kind of goes against what Grails provides for you with its layouts and templates.
If you're trying to "require", say, "blog-header.php" in all of your GSPs, you'd more likely want to just include the contents of the header within a layout, e.g. grails-app/views/layouts/main.gsp, and then use that layout in the views that require the header.
Maybe you should try grails 'template'.
A template is a fragment of view that can be used any time in any view, this is to promove DRY..
<g:render template="myTemplate" model="['object1':object1,'object2':object2]" />
In this case should exists a GSP called _myTemplate.gsp and in this template is working with two objects, you could do a template with only content and no treating objects if you wish.
In the above call the template is invoked in a view in the same folder with the template but you could call it from another view in other folder:
<g:render template="other/myTemplate" model="['object1':object1,'object2':object2]" />
And the pattern for the name of template is the same...
Check it out...

Javascript in an external file

Before I ask this question I must point out that I have tried to search for EVERYTHING!
My question is how can I run javascript from an external file instead of inside my php / html. What I'm trying to do is.
function ClearForm() {
document.form.message.value="";
}
function comeBack(){
if (document.form.message.value == "") {
document.form.message.value="What's on your mind?";
}
}
I have included<script type="text/javascript" src="javascript.js"></script> in the <head> and I have a file in the root called javascript.js and my php file is in the root too so that shouldn't be the problem! But how do I run that pieces of code you see above in the javascript.js file instead of in my php file. It work's fine if I have it in the php file but I want to separate things!
I have also tried to give the form / input field an id and then use getElementById in the external JavaScript file.
But as you can see and hear I'm kinda new to JavaScript so I'm apparently doing something wrong here.
If the above code is the only thing in your Javascript.js file, then you need to call the functions to run the code.
You've included the external Javascript file correctly - however, because all of your JS is included within functions, these function/s must be called before the code will run.
A call to 'ClearForm()' or 'comeBack()' from within your PHP file should run the code.
That JS file will have to be in the same folder as your PHP page.
Test whether the file is found or not by adding this line at the top of the js file
alert('js file found OK!');
document.form is an array, so if you only have one form use:
document.forms[0]
Also depending on which browser you use, find and install some Developer Tools to help you identify these errors.
You have declared those functions in the <head>. All fine.
The question is when do you want to call/run those functions?
If you simply want to run them at the end of the page, then you can add another external javascript file and include it using <script src="my_external_file.js"> right before the </body> tag.
Otherwise, you have to declare onXXX handlers, like onLoad() for the document, onClick() for certain elements, onSubmit() for forms, etc. These, too, can be declared in an external file, but specified after the relevant elements are loaded.

Zend Framework: Enable jQuery in single view

Currently working with ZF and ZendX_JQuery.
As I understand, script links to Google's CDN JQuery are only included in the <head> section of a view if ZF JQuery is referenced in some way in the MVC.
However, I'm working with 'Cloud Zoom', a JQuery library for image zooming.
The view I wish to include Cloud Zoom in has no reference to JQuery, and therefore the script links are not included in the head. How can I make ZF include the script links in the head section of the page without explicitly including any ZF JQuery references in the MVC?
from http://framework.zend.com/manual/en/zendx.jquery.view.html
To access the javascript we have to
utilize the jQuery() functionality.
Both helpers already activated their
dependencies that is they have called
jQuery()->enable() and
jQuery()->uiEnable()
i have this code in my layout
if ($this->jQuery()->isEnabled()) {
$this->headScript()->appendFile('/scripts/jquery.ui.datepicker-it.js');//localizazione di DatePicker
echo $this->jQuery()->setRenderMode(ZendX_JQuery::RENDER_JAVASCRIPT | ZendX_JQuery::RENDER_JQUERY_ON_LOAD);
}
so i guess you have to do something like $this->jQuery()->enable()->enableui()

Is there any way to find in which file - file is included

Am sure the question is vague.
Let me try to explain.
Assume zend frame work - PHP - jquery combination.
I include jquery files in layout.phtml.
i include some files in controller.php.
some file in view.phtml
Atlast when i run and view the page . Is there any way or any tool to find which file is included through which file (layout controller or view) ??
In addition can some one explain which is the best way include js files and where . using zend framework in layout or controller or view
The only way to find where a public, static asset (JS, CSS, image, etc) is included is to trawl through the source code (using something that can "find in files" would save time).
In regards to how and where to include such assets... for global includes (common stylesheets, scripts, etc), include these in your layouts.
For specific page includes, place these in your views.
The best way to include a static asset is using the appropriate view helper. These are generally displayed in your layout file, for example
<?php echo $this->doctype() ?>
<html>
<head>
<?php
echo $this->headMeta()->prependHttpEquiv('Content-Type', 'text/html; charset=' . $this->getEncoding());
// I use "prepend" here so it comes before any page specific stylesheets
echo $this->headLink()->prependStylesheet($this->baseUrl('/css/common.css'));
echo $this->headScript();
?>
</head>
<body>
<!-- content -->
<?php echo $this->inlineScript() ?>
</body>
</html>
You can then add to these placeholders in your view scripts, for example
<?php
// index/index.phtml
$this->inlineScript()->appendFile('https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js')
->appendFile($this->baseUrl('/js/my-jquery-script.js'));
To "include" a file means very different things in PHP (where it is analogous to copying and pasting source code from another file into the current file) and HTML/JavaScript (where you are referencing another file, which the browser must make a separate HTTP request to download). What do you consider "including"? Are image tags "including" the images? At least we can easily count those references by examining HTTP requests; from the client side, it's impossible to tell what include()s went into the source code behind the rendered output. Even naive source code searching couldn't tell you thanks to autoloading. As is, your question is not well enough defined to provide a clear answer.
Controversal answer:
You don't need that.
If you need that then it's something wrong with the way your designed your application.
Note: I've learned (trial and error) that 90% of things I don't know how to do and that seem to be impossible in ZF are a result of wrong application design.

Using php as external javascript with CakePHP?

PHP files can be used as external javascript files. Basically make a php file output valid javascript and use that php file as your javascript file: http://www.javascriptkit.com/javatutors/externalphp.shtml . Can this be done with cakephp since we don't specify php files in the browser but rather a directory based on controllers and their actions?
Late answer, but anyway, this is how I did it.
When linking to external javascript file, don't forget to set inline to false like the one shown below:
$this->Html->script('scriptname', array('inline' => false));
Sure, as long as you output valid JS, id does not matter what the URL looks like and what is behind that URL.
When you link a javascript file with
$this->Html->script('scriptname');
all that happens is that a tag is created in the HTML
<script type="text/javascript" src="path/to/webroot/js/scriptname.js"></script>
So, you can link whatever you'd like.

Categories