Jump to content

EyeDentify

Members
  • Posts

    146
  • Joined

  • Last visited

  • Days Won

    2

Everything posted by EyeDentify

  1. Hello Fellow PW fans. I have since i started using PW allways been a bit afraid to use the API:s more advanced functions. Like creating pages or subpages on the fly in my PHP code in the page template file. So i read alot and finaly got a Eureka the other day. And have now tinkered around a bit and this simple example is the result. It´s just the bare basics. You could make this more advanced in infinity. I wanted to put this example somewhere for the future because i don´t think there is such a clean to the point example anywhere on the forums. All the others are deep within discussion threads with advanced stuff that are not of concern for people who looking for a simple example to start from. Notes: You should log in as admin before trying to run this code in your page template. I put the parent page i use in my testing as unpuplished for security. As allways, the API reference: https://processwire.com/api/ref/ is allways great to have when tinkering with the API. Any ways, here it comes, and you have to change paths and such to your requirements. <?PHP /* some random strings and stuff for testing */ $randTitle = 'test_' . mt_rand(0, 1000000); $randHash = 'test_' . md5(mt_rand(0, 100000) + time()); /* Example: Creating a new sub page to a parent page via API Author: EyeDentify This example do not check if the page name allready exist it just tries to save it no mather what. so you have to create that check your self. */ /* get parent page object to add sub page object to */ $addParent = $pages->get('/api-test-start/'); /* Instantiate a new page object */ $newPage = new Page(); /* turn output formating of for property manipulation */ $newPage->of(false); /* set the page objects parent so it is saved in the right place */ $newPage->parent = $addParent; /* set the name of the template to be used wich we uploaded before hand. */ $newPage->template = 'tmp_api_test_post'; /* sanitize and set page name and also used in the path */ $newPage->setName($sanitizer->pageNameUTF8($randTitle)); /* the title */ $newPage->title = $sanitizer->text($randTitle); /* set custom fields propertys, the ones we created ourselfs */ $newPage->api_test_hash = $randHash; /* save the page object to database as child of the given parent */ $newPage->save(); /* turn on output formatting again, importent because it should be ON when outputting it in template files later on. */ $newPage->of(true); ?> This is kind of a example and code template in one. I hope this can get others started that are beginners like myself to get more advanced with the PW API. I expect there is propably things that should be done a certain way but this is my way. Good luck with your API adventures. Update created a gist for the github users of the PW community of this example. https://gist.github.com/magnusbonnevier/4dbb3a28634a9b76bbf5855dd871606f
  2. I am sure it does. But the point here is that i made something because i wanted to try it with PHP and to do one thing very good. Not everything has to be a module. This script is not meant to be a swizz army knife. The point is to experiment, and share my findings so others could see have they could use PHP in different ways.
  3. The thing is, that people have different ways of working. I prefer to use just plain old CSS from the start and do not feel its worth messing around with Less/scss or similar thing. Thats were my way of using this PHP script come into play. But as i said, thats the beauty with the our age of web development that you can choose the way that works best for you. And you can run all sorts of string functions on the main css file when its been merged to lets say minizise and such. I know you can do that with your workflow as well. But i also see this as an oportunity to use PHP do to different things. Besides i like to experiment.
  4. I Think you have missunderstood the use of this script. It´s just a dev tool for handling the CSS merging to a single file. Just meant for the developer and not the users of the site. The user will only experience the merged file with all the CSS that controls the sites look and feel. Update: i see what you mean by markup cache now. Missunderstood you.
  5. Hello. Perhaps it can lead to confusion. But my reason for using the term "module" is that the goal of the CSS modules as i call them, is for them to be independent of the other ones. There for i use the term module. I use this reasoning after reading the MaintainableCSS guide i link to in my post. But you can call it what suits you best and you feel comfortable with. Its just a term i use for myself to understand what the intention are of the module files.
  6. Hello There PW fans and users. I Thought i share something that i came up with a while ago when i decided to adopt the teachings in this guide: MaintainableCSS This guide has made my development much easier cause i allways was stressed about having all code in one giant file and jumping around trying to keep everything organized and clear. So i decided to go with modules, as i call them, having all the CSS belonging to a specific module in one file, and also the responsive media querys that belongs to the module in the same file. Now... so far so good. Turns out that there is a 31 file limit in browsers to how many files you can load with css. And also i wanted to make less HTTP requests, so i started thinking, and came up with a simple PHP script that i run on my PW site in a hidden template and when that PHP script runs, it combines/compiles or merges the CSS module files together to one giant file that i link to in my website header. This way, there is one HTTP request for all the CSS that runs the site, minus some third party stuff. Lets look at the PHP script <?PHP /* EyeDentifys CSS File combiner. v1.0 Array of essential files - update as needed. We do this to ensure the order of how the files are combined. You can have as many CSS module files here as you want. Remember that the combined file is compiled in the order they are placed in the array. Remember to check all the paths so they reflect your setup before use. And also, the combined file is overwritten without notice when you re-combine. */ $cssModules[] = 'module_global.css'; $cssModules[] = 'module_table.css'; $cssModules[] = 'module_forms.css'; $cssModules[] = 'module_layout.css'; /* init vars */ $str = ''; $moduleCount = 0; $errorCount = 0; $listFiles = ''; /* add a string with the latest update time and date to the top of the combined file */ $dateStr = date('Y-m-d H:i:s'); $str .= '/* Combined file last updated at ' . $dateStr . ' */' . PHP_EOL; /* go through modules and concatenate them in a string var */ foreach($cssModules AS $key => $module){ /* check if the file exist */ if(file_exists('css/modules/' . $module)){ /* read file data */ $str .= file_get_contents('css/modules/' . $module); /* add module count - used for output in template */ $moduleCount++; /* add the file to list */ $listFiles[] = '[' . $module . ']'; } else { /* if the file do not exist add it to the "do not exist" list */ $listFiles[] = 'Error - file [' . $module . '] do not exist!'; /* increment error count */ $errorCount++; } } /* render the combined css */ echo('<h2>Combined CSS</h2><pre class="code-view-pre">' . $str . '</pre>'); /* list combined files */ echo('<h2>Combined files</h2>'); echo('<ul class="unstyled-list">'); foreach($listFiles AS $key => $file) { echo('<li>' . $file . '</li>'); } echo('</ul>'); echo('<p>Number of file errors <strong>' . $errorCount . '</strong> st.</p>'); /* the file name defaults */ $combinedFileName = 'css/combined_styles.css'; $backupFileName = 'css/backups/backup_styles_' . date("Y-m-d_H-i-s") . '.css'; /* backup the old combined file before updating it */ copy($combinedFileName, $backupFileName); echo('<p>Backup created to file: <strong>' . $backupFileName . '</strong></p>'); /* create update the combined css file */ file_put_contents($combinedFileName, $str); echo('<p>Combined css file: <strong>' . $combinedFileName . '</strong> updated!</p>'); echo('<p><strong>' . $moduleCount . '</strong> files combined.</p>'); ?> Like i said above, i have this in a hidden Template file that i run when logged in as ADMIN. I just refresh the page everytime i made a change and uploaded to the server so the new changes is combined into the big file and can be seen on the website. My script assumes the following directory structure: 1. This is where the script looks for the module files: templates -> css -> modules 2. This is where the script saves the combined big CSS file: templates -> css 3. This is where the script saves a backup of the current big CSS file before creating and writing over the new big file: templates -> css -> backups You need to go through the script and change the hardcoded paths to suit your needs before use. Also please try this out at some test site first so you get the hang of it. Be safe. This way of dealing with alot of CSS code has realy made my life much easier. Cause if i want to change some modules CSS i just open that file, make the changes, upload the module, and run the script and its compiled in with the other CSS in the big file. I am sure you creative fellows on the forums can make refinements to this code, but it has served me well. Hope it helps someone.
  7. Hello Robin. Thank you for your insights. I am going to have a look at that first link, cause i must have missed that in the new API docs. I am not expecting PW to docs to give me everything. I just want the info specific to PW in a central place where i can go when i have a question without pestering folks in the forums when there allready is a documented guide or answer. Like how to use the "hooks" and when not to and how to use all the classes and whatnot in PW to create pages with templates and so on. Perhaps that will be in the new API docs also one day. I Rather read documentation from the creators/maintainers of PW and get quality advice then outdated info spread across the forum. Sometimes i just need a little help on the way and i can figure out the rest. That being said, i love the new API docs and it would be superb if this where to be the central place for all relevant documentation. And the fundamentals of OOP in PHP i have found this website to be pretty informative for the beginner like me: http://phpenthusiast.com/ I am not trying to sound ungratefull to the comunity when asking for more Documentation, but PHP would not be where it is without PHP.net that i use constantly when dealing with PHP in my programming projects. See ya around forums.
  8. Hello. Thx for your answer. But i was more looking for along the lines of a central place on the PW website where all documentation and guides pertaining to Module development could be found. Also curiated and verified to be accurate and up to date with latest versions of the PW branches. - Guides ranging from beginner to advanced. - Detailed info on what do to and not to do in module development. - Kinda like the new API docs wich i find absolutly essential for my development these days. But as stated above, PW community are missing a good central place where all the info that has to do with module development can be found. I do not want to plow through endles of forum threads only to have to put together a puzzle to make sense of the information. Plus running the risk of it being outdated. I would love such a place on the main PW website and i am sure many others would. Because if we had all the info in one place their would be less of threads that come up time and time again where beginners like me asked questions that allready had the answers in one central place. I have a basic grasp on PW and building sites with it, but are looking to get a bit advanced creating a module, and a reference would be great. Also a deeper reference how to work with the API, like how you interact with the API outside the Admin software. From basics to the advanced, what to think about and so on. And i am talking about things like saving, creating, updating info to templates or pages from your own custom forms or using PW forms and such. The API stuff about getting info and displaying i allready know and that works. But the more advanced stuff is eluding me. Every aspect realy. Right now the new API docs are really helpfull, but for us a bit more beginners, the lack of more detailed guides and examples are missing and you have to go a bit by trial and error or scour the forum for a few hours just to get some basic understanding without knowing why things are done in a certain way. I would like to have this knowledge so i can evolve and become better at PW and also understand all its nuts and bolts from the inside out. Sorry for my long ramblings. Love the PW community but we need more DOCs in all areas of PW, the new API docs are a start. I hope some of the old foxes and the gurus on here would like to come togheter and create something alone the lines that i mentioned. See you all on the forums. Thx for reading. /EyeDentify
  9. This URL seems not to be valid anymore. I get an error trying to access it. Thought that you should Know cause the info IT provides is helpfull. Maybe the plan is to move that info to the main pw website ?
  10. No problem sir. We are all here to help each other if we can.
  11. You are absolutly right... but it is the default behavior of siblings() to return current page. I have to specify "false" to make it not do that. I missed it. Thank you for your catch https://processwire.com/api/ref/page/siblings/ I think you gave me the way to a solution. solution: <?PHP $sibItems = $page->siblings(false); ?> And now it works as i expected
  12. Hello There fellow PW users. I have been racking my brain looking at this code for a while know and the solution probably is simple. But i need a fresch set of eyes on my problem. I have this code: <?PHP /* render siblings or not */ $sibItems = $page->siblings(); if(count($sibItems) > 0) { echo('<ul class="unstyled-list">'); echo('<li>Other post like this:</li>'); /* render the siblings if any, exclude current page */ foreach($page->siblings('limit=5', false) AS $sibling) { echo("<li><a href=\"{$sibling->url}\">{$sibling->title}</a></li>"); } echo('</ul>'); } ?> What i am trying to do is detect if there are any siblings to the current post, and if there are siblings then render them. If no siblings then not render. You get the picture. But at this point the IF statement is run no mather what i do. and this post i am testing on has no siblings. It is a lone child under another page so it should not have siblings. But the count() method gets a 1 integer anyway ? So what am i doing wrong here? I thought it should give me a 0 integer if there are no siblings. Maybe i have forgotten something. Thanks for all your help.
  13. Thank you all for your tips and answers. I think i might have explained things wrong. The only purpose i have is for me to have a way to organize things so its more clear. Like in my Editor or in my FTP client. Not that i wish to switch certain template files for specific templates and so on. Its purely a organization issue. But i thank you anyway. szabesz seems to come closest to what i was after. I will have a look.
  14. By the way, could you give an example of this "route.php" stuff you mentioned ?
  15. Hello LostKobrakai. Thank you for the fast answer. I will have to give this some thought how to go about it.
  16. Hello Fellow PW users and fans. I have a simple question that needs an answer. Have been working on a pretty complex site know for a while and lots of template files. So i was wondering if i could for sake of keeping everything organized could sort my template files in sub directorys and still have PW recognize these template files and find them when needed ? Or do i have to keep them all in the same "template" dir. I know how to use include() and all that. So thats not what i am after. Because i would like to distinguish my main template files and some that are experimental and possibly not going to be used in the finished site. And there getting to be alot of them at this point and at times a bit confusing to distinguish them. Well there you have it. thanks in advance.
  17. They should rename it so that its apparent that B as in Bold applies the <strong> tag. Cause for me i think tags like <b> and <strong>. Maybe this is why CK editor shuts down when i change that setting ?
  18. Hello. Thank you for your reply. I Rather use strong tag because its what should be used instead of B. But its no big deal, I have worked around it. I was just curios why CK editor would not work when i changed the mentioned setting.
  19. Hello everyone. I am back with a question thats come up lately when i was creating some new fields for a template of mine. The following happend. I created a new field of the "textarea" type and changed it to "CKeditor" on the Details tab under "inputfield type". So far so good. Everything working as expected when editing data on the page using the template wich the field was assigned to. But then i decided to get a little brave, so i went to edit the fields settings and under the "Input" tab i changed "Format tags" string to include the tag "strong". The Format tag field string before my change was: "p;h1;h2;h3;h4;h5;h6;pre;address" and after my change it was: "p;h1;h2;h3;h4;h5;h6;pre;address;strong" Now, after that change when i saved it and went back to editing the data on the page, the CK editor refused to show and there was only the fields label but just white space under it. If i go back and edit the field and the Format tags back to the default everything works again and the CK editor shows up when editing data. But if i change the Format tags it goes away. What i am doing wrong ?
  20. Oki Then i have my answer. I will have to rethink my solution and look at the referenced Ryan Solution wich seems good for my case. Thx Again LostKobrakai.
  21. Thx again. I will have a look at Ryans solution. It seems pretty straight forward to change for my needs. Though i was still curious if there is a strictly PW API way of doing this. if i could specify that i only want to return the "category" field from the pages i target with a selector and that would make it less expensive if i have alot of pages ? If not then i will look at Ryans code and come up with something on my own. I am not that worried about performence right know, but down the line maybe. Thanks again for all suggestions.
  22. Thank you for the post LostKobrakai The link about unique looks promising. But i am still going to first have to return all the records in the PageArray and then filter them with unique ? Is that not expensive when i have alot of pages in that array ?
  23. Hello Again friends. I was working on a new website and i had a mental block about a query i would like to do with the help of PW API. And then i could not figure out how to do this specific one. I know how to do it in MySQL for example: SELECT DISTINCT category FROM movies The purpose here is to get all the uniuqe categorys from those pages that are each one a movie title. Because there could be the same category string in more then one movie title. For example say: 1. Die Hard (category: Action) 2. Die Hard 2 (category: Action) 3. Die Hard 3 (category: Action) 4. Ghosts of Mars (category: Sci-fi) Then i would like to at the end of the query get back: 1. Action 2. Sci-Fi As uniqe categorys. And then make category links using those uniqe categorys wich is no problem either. But how do i perform the above MySQL query in PW API terms ? I Have gone through all Selector documentation and havent found something that directly represent what i want to do. And i know i could use the SQL class and methods, but i want to use PW API as much as possible to learn. I would like to not have to get all the movie title pages with regular PW API and then with some Array magic sort out the uniqe categorys if i could avoid it, cause i know this site will have alot of movies and tv series in the "database" in the future. So that could mean alot of memory usage. Or is my best bet to just have a "tree" structure of say: movies - Action -- Die Hard 1 -- Die Hard 2 -- Die Hard 3 - Sci-Fi -- Ghost of Mars And then say, use a text field on each title page that has the type of either "movie" or "tv series" for example ? Because i want to be able to sort distinguish between them later on. If anything is confusing about my question, just ask and i will try to clarify.
×
×
  • Create New...