Jump to content

"Write some PHP using the ProcessWire API to output the title field"


Recommended Posts

1. Hi, I find this guide: 

http://processwire.com/docs/tutorials/but-what-if-i-dont-know-how-to-code/part-1-pages-templates-fields-files/page4

here there'is write:

"Open your template file which at the moment is completely blank and let’s write some PHP using the ProcessWire API to output the title field. For the moment I am not going to bother with niceties like html. Here is the breakdown of what we will do."

<? Php echo $ page -> titolo ; >?

 

But I don't understand how and where write this php code. Must I open the file .php on my desktop and write from there?
Must I write on it by ftp, go into the server by filezilla?

How to create a file .php in my pc? If I use notepad, the extension remain .txt and not .php 
Can I write the File Template on ProcessWire plattaform?


 

2. The guide to tell than:

 

put the php code "You should now have a new link on the menu called Miy First Page."

BUT I HAVE THIS NEW LINK TO NEW PAGE JUST CREATED, WITHOUT PUTTING NO CODE php anywhere. My .php file VACUUM THAT HAVE LOADED, is REMAINED EMPTY AND I SEE ANY LINK TO THE NEW PAGE PAGE OF DIRECTORS.
So why DRIVING ADVISED TO WRITE THAT CODE ON FILE PHP TEMPLATE?
 
 
 

3. The guide to tell than:

To prove that this is actually a proper page, go back to your template file and add the normal HTML page coding to it (You know, head, body and so on), leaving your little bit of PHP in the body tag. Maybe add <H1> tags before and after the PHP statement? Save that and refresh your browser and you now have a proper html web page with the page title displayed as H1. 

In case you are feeling lazy as hell, you can copy and paste the following, though why I am being so kind to you is beyond me!

<html>
<head>
<title>Hello</title>
</head>
<body>
<h1><?=$page->title?></h1>
</body>
</html>

But Now, I write something in my new page, and I see all without having written anything in the Template File.

 
I have to skip this part of the guide?
Link to comment
Share on other sites

From your explanations I'll conclude your new to programming, at least in php. I'd would advise you to first try some basic entry-level php tutorials, before moving on with processwire, as you won't get far without a basic understanding of php as programming language. To create those php files I would suggest you to use a real programming text-editor (notepad++ is quite nice for beginners, but there are plenty of other ones). This will get you syntax highlighting an other useful features, which help you to find the inevitable errors you'll be making in the beginning. 

About your more specific questions. The folder to put your files into is …/site/templates/ from the root of your processwire installation. I don't know where you installed processwire, but this can be done locally as well as on a server.

As long as a template – created in the backend interface – doesn't have a corresponding template.php file in that folder you cannot view that page in the frontend as processwire won't know how to display the data you filled out in the backend. So about your new page, which did display data: I think that you created a new page of a template, that was already preinstalled by processwire. Those preinstalled templates do also already have corresponding template.php files in place and therefore show up in the frontend. The guide is there to tell you how to move further by creating own templates, that look different from what's already installed.

P.S. Please do not open new threads if you've already one about kinda the same issues. Also please try to avoid writing in uppercase text.

  • Like 4
Link to comment
Share on other sites

Thanks, but since I'm new, I do not know if a "new" problem is similar or related to that of the first. Also, I need many specific details even trivial, as they are just beginners. Is there somewhere a forum other than this to make even trivial questions about processwire?

As for the theme of this post, I have to say that I've created a Template File EMPTY directly on the server, I saw it on PW, I associated with a Template, and despite being empty I have written a post in! Okay now I will try to understand why I did not need to write any code php / html File Template to view this Template ...

Thanks!

Link to comment
Share on other sites

Hi,

Glad you found processwire, a time and headache saving system when compared to others.

Beginners questions are already answered many times in a wide range and to a great extend. Recommended to find your answers in the beginning is to search inside the processwire forum with google.

https://processwire.com/talk/topic/4173-grouped-forum-posts-links-articles-tutorials-code-snippets/

Also, besides the forum, the processwire website holds many information sections:

https://processwire.com/docs/tutorials-old/quick-start/your-first-template/

https://processwire.com/api/templates/

Link to comment
Share on other sites

As for the theme of this post, I have to say that I've created a Template File EMPTY directly on the server, I saw it on PW, I associated with a Template, and despite being empty I have written a post in! Okay now I will try to understand why I did not need to write any code php / html File Template to view this Template ...

Thanks!

You can always fill in data to a template, as the form for that is generated automatically. Only on the frontend this page should be empty (at least have no content if you have prependTemplateFile or appendTemplateFile enabled in the config.php). Templates are only the blueprints to convert the data, that you've saved to a page in the backend, to html code or really any other format you want it to be in. 

excuse me I've another answer, do you prefer Notepad++ or Bluefish??

I'm not using any of those. Just try them out and see which one you like more. Today almost all of those editors do well on basic features, so it's mostly a case of personal preference and maybe advanced features not necessary in the beginning.

Link to comment
Share on other sites

  • 4 weeks later...

Hi franciccio-ITALIANO,

As for the theme of this post, I have to say that I've created a Template File EMPTY directly on the server, I saw it on PW, I associated with a Template, and despite being empty I have written a post in! Okay now I will try to understand why I did not need to write any code php / html File Template to view this Template ...
    Thanks!

ok let me try. :-)

depending on what kind of site-profile you chose on setup, the front-end will be rendered with processwire (pw) like this:

first, pw "looks" for the _init.php file, wherein you could define some stuff that you may want to use all over your site in different places. this usually then includes the _func.php file, where you would define some custom functions you want to use all over your site (building a custom navigation for example)…

then Processwire "looks" for a file that has the same name as the template that is used in the backend (where the input fields are collected).

I think this is the point most confusing to people new to processwire (at least I was confused for a while) -- you have the "template" which is responsible for the set of fields where you put in data. And then you can have a "template-file" - that's the php file with the same name as the "template" plus the .php suffix. (there's an option in the "template" to use a file with a different name, but lets ignore this for now).

so, processwire now looks for this "template-file", but, and that's what confuses you I think - this template-file alone does not render the output you see in the browser, because usually a "_main.php" file is included afterwards - this is where most of the markup and output will happen. So lets say you have a "title" and a "body" field in your "template".

As long as the "_main.php" file uses $page->title and/or $page->body for the output, and as long as your "template-file" does not alter or add something to these variables, you will see your data in the front-end, even if your "template-file" has no code or markup in it.
So what are the "template-files" are for? They serve as the entry points for the data you collect via the fields of the template. Since each template can have a variety of different fields and data-types, somewhere before the output to the page these things must be collected and prepared, like a list of small images that should be wrapped in links to the large image versions for example.

Have a look at the code inside the files that you find in your "...site/templates/" directory, and maybe with the above explanation things will be a little less confusing, I hope. :-)

cheers,
Tom

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...