Working with CodeKit and haml/PHP - php

in my latests projects I have use Rails. Now I have to do something in PHP (which I actually hate, or love too much Ruby syntax).
So now I am very used to work with Sass and haml, which I also love. So I bought CodeKit for doing things outside rails framework.
Wanted just to know if there is any option for use haml in PHP files, or PHP in haml files, and that the file compiles with CodeKit, even having PHP.

I know this is a very late reply, just found this question when searching myself.
In addition to Kevin's reply above:
You can automatically convert the html to php by simply setting the output path for that particular file, and then specifying the extension.
Right click your HAML or HTML
"Set output path..."
"Output filename and extension"
Change it from index.html or index.haml to index.php.

You can use the :plain filter to preserve the php, it does not parse the filtered text. This is useful when you need to keep multiple lines of php.
ex:
:plain
<?php foreach ($es as $e) {
echo $e;
} ?>
I have not heard about any haml/php color syntax for Sublime Text 2 yet.
There is another setting that can be useful for working with php : check the don't escape HTML character in the HAML config on Codekit : http://d.pr/i/4pmv
This will manage with that
%form#booking_log{:name => "booking_log", :method => "post", :action => "<?=$this->action('bookingGetLog')?>"}

Related

Coldfusion and php, in the same file?

On this server, I can use both .cfm and .php files. Both types will be parsed, as expected.
However, I want .cfm files to be parsed for php, as well. For example,
//test.cfm:
<cfoutput>hello from cf</cfoutput>
<?php echo 'hello from php'; ?>
// outputs the php, verbatim, without processing :(
I know that I can change the php config, to parse .cfm. I dont know what order the parsing will take place or any other pros and cons, tricks and tips.
The goal here is that I want to wrap php (which i know well) into a cfm file (much less experience). The cfm file will be in an admin section, which automatically checks the user auth, and includes other cf files.
So, it seems to me that if coldfusion parses the file (checking the user-auth and all that), then hands it over to php, that would be the process that I am looking for.
This has been done:
See: http://www.barneyb.com/barneyblog/projects/cfgroovy2/
Most of the documentation has mixing ColdFusion and Groovy, but other languages can be mixed in too.
Example code:
<cfimport prefix="g" taglib="engine" />
...
<h2>Run some PHP (via Quercus)</h2>
<cftry>
<g:script lang="php">
<?php
$variables["myArray"][] = "Pretty Happy People wrote PHP.";
echo "<pre>";
var_dump($variables["myArray"]);
echo "</pre>";
?>
</g:script>
<cfcatch type="CFGroovy.UnknownLanguageException">
<p>Quercus needs to be added to your classpath for the PHP example to work</p>
</cfcatch>
<cfcatch type="any">
<p>Error running PHP code: #cfcatch.message#</p>
<p>#cfcatch.detail#</p>
</cfcatch>
</cftry>
Source: https://ssl.barneyb.com/svn/barneyb/cfgroovy2/trunk/demo/index.cfm
It will depend on the ColdFusion Application Server, what it supports and what level of interoperability you want. Generally if you want to be able to mix variables across statements, then you need to share the ColdFusion pageContext and make sure that php variables are written and updated. I don't believe that the example above does this, but I am happy to stand corrected. The other alternative is to called ColdFusion from PHP, again using Quercus. These 2 articles will help you do this. The first shows how to call Java from PHP, the second how to call ColdFusion from Java.
http://quercus.caucho.com/quercus-3.1/doc/quercus.xtp#Instantiatingobjectsbyclassname
http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSe61e35da8d318518-106e125d1353e804331-7ffb.html
Firstly create a ColdFusion component that can render ColdFusion dynamically
<cfcomponent displayname="ColdFusion renderer" output="false">
<cffunction name="render" returntype="'String' or 'Any'">
<!---
Do something with the coldfusion code here e.g:
write to a file using <CFFILE> and then <CFMODULE> or <CFINCLUDE>,<CFSAVECONTENT> it
or if the ColdFusion is all script, use the evaluate() function
--->
</cffunction>
</cfcomponent>
the invoke the component in PHP, via Java, using the CFCProxy
<?php
function cfml($code)
{
$cfc = new Java("coldfusion.cfc.CFCProxy", "path/to/cfc/above");
$cfc.render($code);
}
echo 'hello from php';
cfml(<<<EOT
<cfoutput>Hello from CF</cfoutput>
EOT
);
?>

Compressing CSS with no spaces via PHP?

Wondering how to reach a css file like this one from css-tricks.com
http://cdn.css-tricks.com/wp-content/themes/CSS-Tricks-9/style.css?v=9.5
Not sure if he is using php to accomplish this or not. I've been reading countless articles with no luck.
Also, is it something automated that spits out the version number after the .css? Been seeing it around and wondered how to achieve a clean css file.
Any help is appreciated! Thanks.
It's simple enough to use an editor with Search/Replace and strip out all the unnecessary spaces. For instance, when I write CSS I only use spaces to separate keywords - I use newlines and tabs to format it legibly. So I could just replace all tabs and newlines with the empty string and the result is "minified" CSS like the one above.
The version number is a fairly common cache trick. It doesn't affect anything server-side, but the browser sees it as a new file, and caches it as such. This makes it easy to purge the cache of all users when an update is made. Personally, though, I use a PHP function to append "?t=".filemtime($file) (in other words, the timestamp that the file was modified) automatically, which saves me the trouble of manually updating version numbers.
Here is the exact code I use to automatically append modification time to JS and CSS files:
<?php
ob_start(function($data) {
chdir($_SERVER['DOCUMENT_ROOT']);
return preg_replace_callback(
"(/(?:js|css)/.*?\.(?:js|css))",
// all the relevant files are in /js and /css folders of the root
function($m) {
if( file_exists(substr($m[0],1)))
return $m[0]."?t=".filemtime(substr($m[0],1));
else return $m[0];
},
$data
);
});
?>
I would avoid to do it manually because you may corrupt your css.
There are good tools available which will solve such problems for you without to be tricky.
An excellent solution is Assetic which is an assets manager and allow you to filter (minify, compress) using various tools (yuicompressor, google closure, etc..).
It is currently bundle by default with Symfony2 but may be used standalone in any PHP Project.
I've successfully implemented it in a Zend Framework project.

Why is javascript not able to use a javascript variable I declared in a php file?

Hey everybody, this issue has had me stumped for the last week or so, here's the situation:
I've got a site hosted using GoDaddy hosting. The three files used in this issue are index.html , milktruck.js , and xml_http_request.php all hosted in the same directory.
The index.html file makes reference to the milktruck.js file with the following code:
<script type="text/javascript" src="milktruck.js"></script>
The milktruck.js file automatically fires when the site is opened. The xml_http_request.php has not fired at this point.
On line 79 out of 2000 I'm passing the variable "simple" to a function within the milktruck.js file with:
placem('p2','pp2', simple, window['lla0_2'],window['lla1_2'],window['lla2_2']);
"simple" was never initialized within the milktruck.js file. Instead I've included the following line of code in the xml_http_request.php file:
echo "<script> var simple = 'string o text'; </script>";
At this point I have not made any reference whatsoever to the xml_http_request.php file within the milktruck.js file. I don't reference that file until line 661 of the milktruck.js file with the following line of code:
xmlhttp.open('GET',"xml_http_request.php?pid="+pid+"&unLoader=true", false);
Everything compiles (I'm assuming because my game runs) , however the placem function doesn't run properly because the string 'string o text' never shows up.
If I was to comment out the line of code within the php file initializing "simple" and include the following line of code just before I call the function placem, everything works fine and the text shows up:
var simple = 'string o text';
Where do you think the problem is here? Do I need to call the php file before I try using the "simple" variable in the javascript file? How would I do that? Or is there something wrong with my code?
So, we meet again!
Buried in the question comments is the link to the actual Javascript file. It's 2,200 lines, 73kb, and poorly formatted. It's also derived from a demo for the Google Earth API.
As noted in both the comments here and in previous questions, you may be suffering from a fundamental misunderstanding about how PHP works, and how PHP interacts with Javascript.
Let's take a look at lines 62-67 of milktruck.js:
//experiment with php and javascript interaction
//'<?php $simpleString = "i hope this works"; ?>'
//var simple = "<?php echo $simpleString; ?>";
The reason this never worked is because files with the .js extension are not processed by PHP without doing some bizarre configuration changes on your server. Being on shared hosting, you won't be able to do that. Instead, you can rename the file with the .php extension. This will allow PHP to process the file, and allow the commands you entered to actually work.
You will need to make one more change to the file. At the very top, the very very top, before anything else, you will need the following line:
<?php header('Content-Type: text/javascript'); ?>
This command will tell the browser that the file being returned is Javascript. This is needed because PHP normally outputs HTML, not Javascript. Some browsers will not recognize the script if it isn't identified as Javascript.
Now that we've got that out of the way...
Instead I've included the following line of code in the xml_http_request.php file: <a script tag>
This is very unlikely to work. If it does work, it's probably by accident. We're not dealing with a normal ajax library here. We're dealing with some wacky thing created by the Google Earth folks a very, very long time ago.
Except for one or two in that entire monolithic chunk of code, there are no ajax requests that actually process the result. This means that it's unlikely that the script tag could be processed. Further, the one or two that do process the result actually treat it as XML and return a document. It's very unlikely that the script tag is processed there either.
This is going to explain why the variable never shows up reliably in Javascript.
If you need to return executable code from your ajax calls, and do so reliably, you'll want to adopt a mature, well-tested Javascript library like jQuery. Don't worry, you can mix and match the existing code and jQuery if you really wanted to. There's an API call just to load additional scripts. If you just wanted to return data, that's what JSON is for. You can have PHP code emit JSON and have jQuery fetch it. That's a heck of a lot faster, easier, and more convenient than your current unfortunate mess.
Oh, and get Firebug or use Chrome / Safari's dev tools, they will save you a great deal of Javascript pain.
However...
I'm going to be very frank here. This is bad code. This is horrible code. It's poorly formatted, the commenting is a joke, and there are roughly one point seven billion global variables. The code scares me. It scares me deeply. I would be hesitant to touch it with a ten foot pole.
I would not wish maintenance of this code on my worst enemy, and here you are, trying to do something odd with it.
I heartily encourage you to hone your skills on a codebase that is less archaic and obtuse than this one before returning to this project. Save your sanity, get out while you still can!
perhaps init your values like this:
window.simple = 'blah blah blah'
then pass window.simple
You could try the debugger to see what is going on, eg. FireBug

custom php function creation and install

I would like to know how to create a php function that can be installed in php
just like the already built in functions like :
rename
copy
The main point I would like to achieve is a simple php function that can be called from ANY php page on the whole host without needing to have a php function within the php page / needing an include.
so simply I would like to create a function that will work like this :
location();
That without a given input string will output the current location of the file via echo etc
Well, there are a couple of options here. One of them is to actually extend the language by writing an extension. You'd have to muck around with the PHP source code, write it in C, and deal with the Zend Engine internally. You probably wouldn't be able to use this on a shared host and it would be quite time consuming and probably not worth it.
What I would do is put all of your functions into a separate PHP file, say helper_functions.php. Now, go into your php.ini and add the directive: auto_prepend_file = helper_functions.php. This file should be in one of the directories specified in your include_path (that's a php.ini directive too).
What this does is basically automatically put include 'helper_functions.php'; on every script. Each and every request will have these functions included, and you can use them globally.
Read more about auto_append_file.
As others have said, there's probably an easier, better way to do most things. But if you want to write an extension, try these links:
http://docstore.mik.ua/orelly/webprog/php/ch14_01.htm
http://www.tuxradar.com/practicalphp/2/3/0
So you want to extend PHP's core language to create a function called location(), written in C, which could be done in PHP by:
echo __FILE__;
Right. Have fun doing that.

PHP: Where should I store the text for info & warning messages

I have a question concerning the design of my project's Code.
In most cases, it is important to separate content from code (HTML mixed with PHP in bigger apps=No good idea, etc.) but how should I handle things like the text of error messages?
Assuming this one message will be only used in one case/PHP file:
MessageBox( 'Something gone wrong, probably your fault' );
or
MessageBox( Lang::error_usersfault );
(Where Lang is a class located in some config file, full of consts)
If you have any experiences (I think every PHP programmer will come across something like this) with things like in the example above - What are they? How do you solve it?
What is the better solution?
May be you'll find php extension gettext useful?
MessageBox(_("Have a nice day"));
PHP Manual of Gettext::gettext

Categories